如何在我的Web.Config中切换2个连接字符串(为DBML激活一个)

我的DBML(Linq to SQL)有两个连接字符串(在Web.Config:CS_Local和CS_Production中)。

在我的Global.Asax / Application_Start中,如果请求是非本地的(!HttpContext.Current.Request.IsLocal),我会运行一些生产准备方法。 在该部分中,我还想将我的DBML使用的当前连接字符串从标准CS_Local更改为CS_Production。

我该怎么办? 一些帮助请..

您可以使用以下命令动态定义dbml上下文:

string connectionString = HttpContext.Current.Request.IsLocal ? ConfigurationManager.ConnectionStrings["CS_Local"].ConnectionString : ConfigurationManager.ConnectionStrings["CS_Production"].ConnectionString; yourDataContext = new YourApplicationDataContext(connectionString);