在 web.config 中为 dev、qa 和 prod 设置 Entity Framework 连接字符串时遇到问题

Having trouble setting up Entity Framework connection string in web.config for dev, qa, & prod

我继承了一个旧的 vs.net 2010 WCF webservice 项目来进行修复。它在其 web.config 中具有以下 connectionString 设置,以区分开发、质量检查和生产,我在尝试调试时无法连接到它们,希望有人能提供帮助:

  <connectionStrings>    
     <add name="OrderDataContextContainer.Development" connectionString="metadata=res://<conn string here>" providerName="System.Data.EntityClient" />
     <add name="OrderDataContextContainer.QA" connectionString="metadata=res://<conn string here>" providerName="System.Data.EntityClient" />
     <add name="OrderDataContextContainer.Production" connectionString="metadata=res://<conn string here>" providerName="System.Data.EntityClient" /> 
 </connectionStrings>

我添加的代码通过以下 "using" 语句建立数据库连接:

using (OrderDataContextContainer db = new OrderDataContextContainer())
 {
      //my code fix here
 }

如果我将以下 web.config 连接添加到 connectionStrings 列表,它连接正常(即没有附加 "Development"、"QA" 或 "Production"):

<add name="OrderDataContextContainer" connectionString="metadata=res://<conn string here>" providerName="System.Data.EntityClient" /> 

但是,我不能这样做,因为当我将此修复程序发送给 QA 团队进行测试时,我的公司需要单独的 dev、qa 和 prod 字符串。如果我不包括上述连接,我会在该 "using" 语句中收到以下错误:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

在 运行 项目之前,我在我的 vs.net 配置管理器中创建了 "Development"、"QA" 和 "Production" 配置并将其设置为 "Development" 但运气不好。

知道如何让我的应用程序通过前面提到的连接字符串进行连接吗?

那是因为 EF 总是在 .config 文件中搜索相同的连接字符串。该名称在 EF 设计器中定义,通常与上下文同名 class.

您可以做的是使用 web.config 转换,关于如何在您的项目中进行转换:How to: Transform Web.config When Deploying a Web Application Project