正如标题所示,我在持久数据库连接方面遇到了一点问题。在这里,我们有 2 个数据库,一个用于开发和测试,一个用于官方生产。我们曾经在测试和部署时手动更改不同的连接字符串,有时会发生奇怪的错误。当我们突然停止开发数据库时,整个生产应用程序停止工作,说 SQL 连接丢失。但是 web.config 指向的是生产环境而不是测试环境!
经过一番研究,我们发现了 web.config 转换并认为可以解决我们的问题。它没有。即使我们的 web.config 没有对它进行单一引用,问题仍然存在,就好像我们的应用程序与开发环境紧密相关。
为了让事情更清楚,我将发布连接字符串、转换和发生的最新 SQL 错误(当我们将开发数据库限制为单用户进行某些更新时),以及我们将其连接到 DBML (L2SQL) 的方式.
连接字符串
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<!--ConnectionString Central-->
<add name="OperationsBD" connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=OPERATION;Persist Security Info=True;User ID=lab;Password=devPassword" providerName="System.Data.SqlClient"/>
<add name="AnalisysBD" connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=lab;Password=devPassword" providerName="System.Data.SqlClient"/>
<!--Production ConnectionString-->
<!--<add name="OperationsDB" connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=OPERATION;Persist Security Info=True;User ID=company;Password=prodPassword" providerName="System.Data.SqlClient"/>
<add name="AnalisysDB" connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=company;Password=prodPassword" providerName="System.Data.SqlClient"/>-->
WEB.CONFIG 转换(生产)
<connectionStrings>
<add name="OperationsDB"
connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=OPERATION;Persist Security Info=True;User ID=company;Password=prodPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="AnalisysDB"
connectionString="Data Source=PRODSERVER\COMPANY;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=company;Password=prodPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
WEB.CONFIG 转换 (DEV)
<connectionStrings>
<add name="OperationsDB"
connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=OPERATION;Persist Security Info=True;User ID=lab;Password=devPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="AnalisysDB"
connectionString="Data Source=DEVSERVER\DEV;Initial Catalog=ANALISYS;Persist Security Info=True;User ID=lab;Password=devPassword"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
SQL ERROR(开发环境的单用户限制)
Cannot open database "operation" requested by the login. The login failed. Login failed for user 'lab'.
最后,我们将它连接到 LINQ to SQL DBML 的方式
public partial class OperationDataContext
{
private const string OPERATION_CONN_STRING = "OperationsDB";
public OperationDataContext()
: base(System.Configuration.ConfigurationManager.ConnectionStrings[OPERATION_CONN_STRING].ConnectionString, mappingSource)
{
OnCreated();
}
}
public partial class AnalisysDataContext
{
private const string ANALISYS_CONN_STRING = "AnalisysDB";
public AnalisysDataContext()
: base(System.Configuration.ConfigurationManager.ConnectionStrings[ANALISYS_CONN_STRING].ConnectionString, mappingSource)
{
OnCreated();
}
}