简体   繁体   English

无法在web.config中为dev,qa和prod设置Entity Framework连接字符串

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

I inherited an old vs.net 2010 WCF webservice project to make a fix to. 我继承了一个旧的vs.net 2010 WCF Web服务项目来进行修复。 It has the following connectionString settings in its web.config, to differentiate between dev, qa, & production & I'm having trouble connecting to them when trying to debug & hoping someone can help: 它在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>

The code I added, makes a db connection via the following "using" statement: 我添加的代码通过以下“ using”语句建立数据库连接:

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

If I add the following web.config connection to the list of connectionStrings, it connects fine (ie no "Development", "QA", or "Production" appended): 如果我将以下web.config连接添加到connectionStrings列表中,则表示连接正常(即未附加“开发”,“ QA”或“生产”):

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

However, I can't do it like this because my company requires separate dev, qa, & prod strings for when I send this fix to the QA team for testing. 但是,我无法做到这一点,因为当我将此修复程序发送给质量检查小组进行测试时,我的公司需要单独的dev,qa和prod字符串。 If I don't include the above connection, I receive the following error on that "using" statement: 如果我不包括上述连接,则在“使用”语句上收到以下错误:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid. 在配置中找不到指定的命名连接,或者不打算与EntityClient提供程序一起使用,或者无效。

Before running the project, I created "Development", "QA", & "Production" configs in my vs.net Configuration Manager & set it to "Development" but no luck. 在运行项目之前,我在vs.net配置管理器中创建了“开发”,“ QA”和“生产”配置,并将其设置为“开发”,但是没有运气。

Any idea how I can get my application to connect via the earlier mentioned connection strings? 知道如何让我的应用程序通过前面提到的连接字符串进行连接吗?

That's because EF always searches for the same connection string in the .config file. 这是因为EF总是在.config文件中搜索相同的连接字符串。 The name is defined in the EF designer and usually has the same name as the context class. 该名称在EF设计器中定义,通常与上下文类具有相同的名称。

What you can do, is use web.config transformations, on how to do it in your project: How to: Transform Web.config When Deploying a Web Application Project 您可以做的是在项目中使用web.config转换,方法是: 如何:在部署Web应用程序项目时转换Web.config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM