45

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.

2 Answers 2

67

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>
3
  • The appSettings part is optional depending on how you set up your connection.
    – sbeskur
    Commented Dec 3, 2011 at 6:54
  • 1
    What exactly does that appSettings part do? Also, is it safe to assume that I can encrypt my transformed connectionStrings using aspnet_regiis -pef? Commented Dec 3, 2011 at 7:18
  • I use the appSettings in case I have multiple connection strings Local Db vs one hosted on a different machine. It's purely convenience driven so that I could use the appsetting to decide which db use. ( I really should just have left it out of this example)
    – sbeskur
    Commented Dec 6, 2011 at 0:22
7

You shouldn't need to create a new file, it should be in the Solution Explorer, expand Web.config, and open Web.Release.config.

Scott Allan has a good video on it here (under Configuration and Deployment > Config Transformations).

4
  • Any chance you could re-link to the video? Looks like pluralsight have changed their catalogue structure since 2011 :-(
    – immutabl
    Commented Feb 25, 2014 at 14:00
  • 1
    This video was just what I needed. Thanks.
    – MartinJH
    Commented Oct 30, 2016 at 17:31
  • 1
    Link is to a pay wall. Commented Jan 9, 2020 at 0:35
  • Damn, that link used to go to a free video. Sorry Commented Mar 13, 2020 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.