本文介绍了的ConnectionStrings configSource在App.config中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的连接字符串从我的App.config中分离出来,并作为你不能做的转换像Web.config中,我想可能我可以使用 configSource 属性指向与连接字符串的另一个配置文件,但它似乎并不奏效。

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 configSource attribute to point to another config file with the connection string in, but it doesn't seem to be working.

本作品,的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>

不过,这不,的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

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

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

I'm looking for the simplest of solutions.

任何想法?

推荐答案

如果你自己,生成操作(在文件属性)添加的文件可能没有正确设置。

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

复制到输出目录选项必须是复制如果新复制始终,以确保的.config 文件结束了在目录,否则会不应该存在,并试图加载配置将会失败。

The Copy to Output Directory option needs to be Copy if newer or Copy Always to ensure that the .config file ends up in the bin directory, otherwise it will not be there and trying to load the configuration will fail.

这篇关于的ConnectionStrings configSource在App.config中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:20