Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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.

like image 788
Mike Bailey Avatar asked Dec 03 '11 06:12

Mike Bailey


People also ask

How do I reference a connection string in web config?

Instead use the connectionStrings section in web. config. To read the connection string into your code, use the ConfigurationManager class. string connStr = ConfigurationManager.

How does web config transform work?

A Web. config transformation file contains XML markup that specifies how to change the Web. config file when it is deployed. You can specify different changes for specific build configurations and for specific publish profiles.


1 Answers

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> 
like image 155
sbeskur Avatar answered Sep 22 '22 21:09

sbeskur