How can I switch between 2 Connection Strings in my Web.Config (Activate one for DBML)

纵饮孤独 提交于 2019-12-21 06:43:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!