Reputation: 585
I have asked this question What is the best way to dynamically load connection strings
But maybe it's better to ask the question a little different.
I have a CRM solution where I have different SQL databases for each company, but I would like to try to only have one IIS site, since the site is always equal except for the SqlConnectionString in the Web.config file. I use LinqToSql and generates the classes automatic using the tools in VS2008. This means that I have the DataClasses which is automatic generated. I found that this class reads the connection string from the web.config in this line:
public DataClassesDataContext() :
base(global::System.Configuration.ConfigurationManager
.ConnectionStrings["TheConnectionString"].ConnectionString,
mappingSource)
{
OnCreated();
}
The users all logs into the same IIS site which I want to authenticate against AD and then redirect them to the CRM site, when I do I would like to dynamic get the right ConnectionString and make the Linq class use that.
Am I on the right track to make this happen? And if so, how do I achieve to load the connectionstring dynamic for the linq classes, the file which the code above is found is autogenerated and is deleted every time I compile.
Any ideas?
Upvotes: 0
Views: 201
Reputation: 1421
You can do that if in linqmetal desinger click on it and then in the property window on sql connection you set to blank so the autogenerate dont owerwrite you code. Then you create partial class for you datacontex and then you create this contructor by your self example:
public partial class DataClassesDataContext
{
public DataClassesDataContext()
: base(StaticMethodThatYouDoLogicForConnStringDecision(), mappingSource)
{
}
}
Upvotes: 2
Reputation: 273199
With both ADO entities and Linq-toSQL the (generated) datacontext class has an overload that accepts a ConnectionString.
So you will have to add a few steps each time you create the context:
string myConnectName = Lookup[Session.ConnectName]; // or something
var rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var connString = rootWebConfig.ConnectionStrings.ConnectionStrings[myConnectName];
var context = new MyDataContext(connString.ConnectionString);
Upvotes: 1