App.Config 转换无效

App.Config transformation not working

我试图在我的构建服务器上动态更改 app.config,我创建了一个名为 Build 的配置,并且我有这 4 个 .config 文件。

这是我的app.config

<connectionStrings>
    <!--configSource="connectionStrings.config"-->
    <add name="TestContext" connectionString="Data Source=ServerDoesNotExist;Initial Catalog=TestDb; Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>

和我的 app.build.config

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionstrings xdt:transform="Replace">
      <add name="TestContext" connectionstring="Data Source=REALEXISTINGSERVER;Initial Catalog=TestDb; Integrated Security=true" providername="System.Data.SqlClient"/>
  </connectionstrings>
</configuration>

但是,当我创建构建定义并添加 configuration =Build 时,出现此错误

Error: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The network path was not found

这意味着采用 app.config 文件而不是转换

您可以使用 SetAttributes

<connectionStrings>
    <add name="TestContext" connectionString="Data Source=REALEXISTINGSERVER;Initial Catalog=TestDb; Integrated Security=true" providername="System.Data.SqlClient" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

请参考

How do I use Web.Config transform on my connection strings?