Dev数据库连接在生产环境中持久存在

时间:2013-04-23 18:30:33

标签: asp.net-mvc-3 sql-server-2005 web-config

正如标题所示,我对持久数据库连接有一点问题。在这里,我们有2个数据库,一个用于开发和测试,另一个用于生产。我们曾经在测试和部署时手动更改不同的连接字符串,有时会发生奇怪的错误。当我们突然停止dev数据库时,整个生产应用程序停止工作,说SQL连接丢失了。但web.config指向生产而不是测试环境!

经过一番研究,我们发现web.config转换并认为可以解决我们的问题。它没有。问题仍然存在,即使我们的应用程序对开发环境很紧张,即使我们的web.config没有对它进行单一引用。

为了使事情更清楚,我将发布连接字符串,转换和发生的最新SQL错误(当我们将dev数据库限制为单用户进行某些更新时),以及我们将其连接到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 TRANSFORMATION(PRODUCTION)

<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 TRANSFORMATION(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错误(对开发环境的单用户限制)

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();
        }
    }

1 个答案:

答案 0 :(得分:0)

这可能只是一个错字,但我注意到Dev上的“Name”是“OperationsBD”和“AnaylsisBD”,但是Transforms被设置为匹配“OperationsDB”和“AnaylsisDB”的名称。如果这不是拼写错误,它永远不会匹配,因此永远不会替换连接字符串。