我可以看到 Web.config
文件包含两个文件:
-web.Debug.config
-web.Release.config
在此配置文件中,有以下注释:
In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
什么是
"Match" locator
?我已经在
Web.config
中有一个连接字符串那么我该如何设置呢?主 web.config 文件应该包含生产连接字符串还是其他方式?
我正在寻找做过类似事情的人的建议
最佳答案
我们正在使用 xdt:Transform="Replace",它基本上替换了我们开发数据库的连接字符串,并且运行良好。见下文:
开发连接字符串(在您的情况下为 web.Debug.config):
<connectionStrings>
<add name="MyDB" connectionString="Data Source=DebugSQLServer;Initial Catalog=MyDebugDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
生产连接字符串(在您的情况下为 web.Release.config):
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings>
关于asp.net - 发布项目时如何交换连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19324470/