我目前正在使用Visual Studio 2010.我想为“测试”配置设置不同的连接字符串。我尝试过使用配置转换,但它似乎不起作用。
在我的 Web.config 中,我的连接字符串如下:
<add name="MyDb" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevelopmentDb;Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
我发现你可以通过添加配置转换来替换Web.config中定义的连接字符串。我右键单击Web.config并单击“添加配置转换”。这创建了 Web.TEST.config 文件。然后我添加了一个连接字符串替换,但它似乎没有工作。它仍然使用 Web.config 中定义的连接字符串。
Web.TEST.config 的文件内容为:
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
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=.\SQLEXPRESS;Initial Catalog=TestDb;Integrated Security=SSPI; MultipleActiveResultSets=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
对于我可能遗失的任何想法?
答案 0 :(得分:1)
转换在部署项目时应用,而不是在编译时应用。
如果您使用的是IIS,则可以部署到配置的Web应用程序目录,这将导致应用转换。