C# 如何在连接字符串上使用Web.Config转换?

C# 如何在连接字符串上使用Web.Config转换?,c#,asp.net-mvc-3,web-config,connection-string,C#,Asp.net Mvc 3,Web Config,Connection String,在我当前的项目中,我有一些对本地开发机器有效的连接字符串: <configuration> <connectionStrings> <add name="ApplicationServices" connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI" </connectionStrings> .... &

在我当前的项目中,我有一些对本地开发机器有效的连接字符串:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=SSPI"
  </connectionStrings>
....
</configuration>


在它上面。

这对我很有效,但我也发现它有时有点像雪花。 您需要创建另一个名为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>

您不需要创建新文件,它应该位于解决方案资源管理器中,展开Web.config,然后打开Web.Release.config


Scott Allan有一段很好的视频(在“配置和部署>配置转换”下)。

appSettings部分是可选的,具体取决于您如何设置连接。appSettings部分的作用是什么?此外,是否可以安全地假设我可以使用
aspnet\u regiis-pef
加密转换后的
连接字符串
?如果我有多个连接字符串本地数据库,而另一个连接字符串托管在另一台机器上,我会使用appSettings。这纯粹是为了方便起见,所以我可以使用appsetting来决定使用哪个db。(我真的应该把它从这个例子中漏掉)你有没有可能重新链接到这个视频?看起来pluralsight自2011年以来改变了他们的目录结构:-(这段视频正是我所需要的。谢谢。链接是到付费墙的。该死,那个链接过去是到免费视频的。对不起
<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>