如何在我的连接字符串上使用 Web.Config 转换? [英] How do I use Web.Config transform on my connection strings?
问题描述
在我当前的项目中,我有一些对本地开发机器有效的连接字符串:
<预><代码><配置><连接字符串><添加名称=应用服务"connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI"</connectionStrings>....</配置>我将如何使用 Web.Config 转换将此表达式转换为对我们的生产服务器有效的表达式?生产服务器看起来像:
<预><代码><配置><连接字符串><添加名称=应用服务"connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"</connectionStrings>....</配置>语法对我来说并不明显,而且我完全无法理解 页面.
这对我有用,但我也发现它有时有点古怪.您将需要创建另一个名为 Web.Config.Release 的文件并用以下内容填充它:
<配置xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><连接字符串><add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/></connectionStrings><system.web><编译xdt:Transform="RemoveAttributes(debug)"/></system.web><应用设置><add key="default_db_connection" value="local" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/></appSettings></配置>
In my current project, I have some connection strings that are valid for local development machines:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI"
</connectionStrings>
....
</configuration>
How would I use the Web.Config transforms to convert from this expression to one valid for our production server? The production server one would look something like:
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
</connectionStrings>
....
</configuration>
The syntax isn't obvious to me, and I'm completely failing at grokking the page on it.
This works for me but I too have found it to be a bit flakey at times. You will need to create another file called Web.Config.Release and fill it with the following:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
<appSettings>
<add key="default_db_connection" value="local" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
这篇关于如何在我的连接字符串上使用 Web.Config 转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!