How do I use Web.Config transform on my connection strings?
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).
Related videos on Youtube
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, 2022Comments
-
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 over 12 yearsThe appSettings part is optional depending on how you set up your connection.
-
Mike Bailey over 12 yearsWhat exactly does that
appSettings
part do? Also, is it safe to assume that I can encrypt my transformedconnectionStrings
usingaspnet_regiis -pef
? -
Galvion over 12 yearsI 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 about 10 yearsAny chance you could re-link to the video? Looks like pluralsight have changed their catalogue structure since 2011 :-(
-
MartinJH over 7 yearsThis video was just what I needed. Thanks.
-
Tomas Beblar over 4 yearsLink is to a pay wall.
-
Garrett Fogerlie about 4 yearsDamn, that link used to go to a free video. Sorry