【问题标题】:Having trouble setting up Entity Framework connection string in web.config for dev, qa, & prod在 web.config 中为 dev、qa 和 prod 设置实体框架连接字符串时遇到问题
【发布时间】:2017-05-14 22:38:46
【问题描述】:

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

  <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 (OrderDataContextContainer db = new OrderDataContextContainer())
 {
      //my code fix here
 }

如果我将以下 web.config 连接添加到 connectionStrings 列表,它可以正常连接(即没有附加“开发”、“QA”或“生产”):

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

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

在配置中找不到指定的命名连接,不打算与 EntityClient 提供程序一起使用,或者无效。

在运行项目之前,我在我的 vs.net 配置管理器中创建了“开发”、“QA”和“生产”配置并将其设置为“开发”,但没有运气。

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

【问题讨论】:

  • 也许这个article 可以帮助你。您需要添加 web.QA.Config 和 web.Production.config。当您开始发布时,您可以选择不同的配置文件(web.config),然后可以转换您的连接字符串。
  • 转换是可行的方法,但如果您想在代码中进行转换,另一种选择是 connection string builder

标签: c# asp.net entity-framework web-config linq-to-entities


【解决方案1】:

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

您可以做的是使用web.config 转换,在您的项目中如何做到这一点:How to: Transform Web.config When Deploying a Web Application Project

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多