Config转换不在单元测试项目中处理连接字符串

时间:2017-12-27 22:44:05

标签: c# unit-testing configuration msbuild

我有一个Visual Studio 2017单元测试项目,其中包含以下App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="SomeKey" value="A" />
  </appSettings>
  <connectionStrings>
    <add name="MyConnection" 
         connectionString="ConnectionToDatabase1" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

然后我添加一个名为App.Release.config的转换文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="SomeKey" value="B" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
  <connectionStrings>
    <add name="MyConnection" 
         connectionString="ConnectionToDatabase2" 
         xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
  </connectionStrings>
</configuration>

所以我希望SomeKey的值为&#34; B&#34;和连接字符串是&#34; ConnectionToDatabase2&#34;。

但是,当我在Release中构建我的测试项目时 - 配置我得到以下*.dll.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="SomeKey" value="B" />
  </appSettings>
  <connectionStrings>
    <add name="MyConnection" 
         connectionString="ConnectionToDatabase1" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

因此,转换适用于appSettings但不适用于connectionStrings。我甚至可以获得价值&#34; B&#34;如果我使用

ConfigurationManager.AppSettings["SomeKey"];

为什么连接字符串没有转换?

0 个答案:

没有答案