问题
I have two connection strings (both in Web.Config: CS_Local and CS_Production) for my DBML (Linq to SQL).
In my Global.Asax/Application_Start I run some production preparation methods if the request is non-local (!HttpContext.Current.Request.IsLocal). Within that part, I'd also like to change the current connection string used by my DBML from the standard CS_Local to CS_Production.
How would I do that? Some help please..
回答1:
You can define the dbml context on the fly with:
string connectionString = HttpContext.Current.Request.IsLocal ?
ConfigurationManager.ConnectionStrings["CS_Local"].ConnectionString :
ConfigurationManager.ConnectionStrings["CS_Production"].ConnectionString;
yourDataContext = new YourApplicationDataContext(connectionString);
来源:https://stackoverflow.com/questions/2885707/how-can-i-switch-between-2-connection-strings-in-my-web-config-activate-one-for