How do I use Web.Config transform on my connection strings?

42,381

Solution 1

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>

Solution 2

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

Share:
42,381

Related videos on Youtube

Mike Bailey
Author by

Mike Bailey

I am (or was) a Physics / Computer Science major with interests in distributed and parallel computing, computational physics, and web application development. I dabble in plenty of Physics related things from time to time, but I mostly spend my time programming in C++, C# and Mathematica. I have a rather obsessive interest in distributed computing and parallel computing in general. My most recent interests are within web application development in the ASP.NET MVC3 framework and in high performance computing with visualization. Disclaimer: The questions and answers provided by me on this website do not represent the views of my employer. They are purely my own and are not meant to represent that of my employer.

Updated on July 09, 2022

Comments

  • Mike Bailey
    Mike Bailey almost 2 years

    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.

  • Galvion
    Galvion over 12 years
    The appSettings part is optional depending on how you set up your connection.
  • Mike Bailey
    Mike Bailey over 12 years
    What exactly does that appSettings part do? Also, is it safe to assume that I can encrypt my transformed connectionStrings using aspnet_regiis -pef?
  • Galvion
    Galvion over 12 years
    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)
  • immutabl
    immutabl about 10 years
    Any chance you could re-link to the video? Looks like pluralsight have changed their catalogue structure since 2011 :-(
  • MartinJH
    MartinJH over 7 years
    This video was just what I needed. Thanks.
  • Tomas Beblar
    Tomas Beblar over 4 years
    Link is to a pay wall.
  • Garrett Fogerlie
    Garrett Fogerlie about 4 years
    Damn, that link used to go to a free video. Sorry