[英]How do I use Web.Config transform on my connection strings?
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? 我将如何使用Web.Config转换将这一表达式转换为对我们的生产服务器有效的表达式? 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:
您将需要创建另一个名为Web.Config.Release的文件,并将其填充以下内容:
<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>
You shouldn't need to create a new file, it should be in the Solution Explorer, expand Web.config, and open Web.Release.config. 您不需要创建新文件,它应该在解决方案资源管理器中,展开Web.config,然后打开Web.Release.config。
Scott Allan has a good video on it here (under Configuration and Deployment > Config Transformations). 斯科特·艾伦(Scott Allan)在此处有一个很好的视频(在“配置和部署”>“配置转换”下)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.