C# App.config 中的 connectionStrings configSource 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14873126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:17:26  来源:igfitidea点击:

connectionStrings configSource in App.config not working

c#.netvisual-studio-2012connection-string

提问by Adam K Dean

I'm trying to separate my connection string from my App.config, and as you can't do transformations like with Web.config, I thought may I could use the configSourceattribute to point to another config file with the connection string in, but it doesn't seem to be working.

我想我的连接字符串从我分开App.config,并且你不能做变换像Web.config,我想可能我可以使用configSource属性点与在连接字符串中的另一个配置文件,但它似乎没有不工作。

This works, App.config:

这有效,App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=*snip*" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="DefaultConnection"
      providerName="System.Data.SqlClient"
      connectionString="Server=*snip*" />
  </connectionStrings>
</configuration>

But this doesn't, App.config:

但这不是,App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=*snip*" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings configSource="connections.config" />      
</configuration>

connections.config:

connections.config

<connectionStrings>
    <add name="DefaultConnection"
      providerName="System.Data.SqlClient"
      connectionString="*snip*" />
</connectionStrings>

I'm looking for the simplest of solutions.

我正在寻找最简单的解决方案。

Any ideas?

有任何想法吗?

采纳答案by Oded

If you added the file yourself, the build action (in the file properties) may not have been set correctly.

如果您自己添加了文件,则构建操作(在文件属性中)可能未正确设置。

The Copy to Output Directoryoption needs to be Copy if neweror Copy Alwaysto ensure that the .configfile ends up in the bindirectory, otherwise it will not be there and trying to load the configuration will fail.

Copy to Output Directory选项需要是Copy if newerorCopy Always以确保.config文件在bin目录中结束,否则它将不存在并且尝试加载配置将失败。

Right Clickon file and then Click properties

右键单击文件,然后单击属性

enter image description here

在此处输入图片说明

Change to "Copy always"or "Copy if newer"

更改为“始终复制”“如果更新则复制”

enter image description here

在此处输入图片说明

回答by onlyme

I had the same issue and Oded solution works for me. But I will just precise that to find out how to change the file "Copy to Output Directory option" to be "copy if newer or copy always", you have to

我有同样的问题,Oded 解决方案对我有用。但我只想知道如何将文件“复制到输出目录选项”更改为“如果更新则复制或始终复制”,您必须

  • rigth click on the file
  • select properties
  • go to advance
  • then will see copy to output directory and choise copy if newer or copy always
  • 右键单击文件
  • 选择属性
  • 前进
  • 然后将看到复制到输出目录并选择复制如果更新或始终复制

This helped me, I hope it will help you too

这对我有帮助,我希望它也能帮助你