Different ConnectionString for publishing/deploying Web Application

Hey there,
I’ve got a quick tip for you guys! Just now I was getting annoyed with the fact that after each filesystem publish I had to go and change the connectionstring to target my server SQL server instance, isntead of my development database. So I googled a while and found the following URL. I haven’t had the time to get it all figured out and tell you all about it but I’ve got it working! ūüôā

Basically what you do is, edit your Web.Release.config file in Vs.net, to tell Vs.Net which nodes in your Web.config need to be replaced before publishing. This file can be found under Web.config. There’s not much here by default, but that’s the point.

So, to update my connectionstring to the proper one for my server I had to do the following.

This is my original connectionstring as it is in my Web.config file:

<pre><span style="color:#339966;">&lt;configuration&gt;
    &lt;add name="EFDbContext" connectionString="Data Source=TOWER\TOWERSQL;
    Initial Catalog=SportStore;Persist Security Info=True;User ID=sa;Password=somepass;Pooling=False" providerName="System.Data.SqlClient"/&gt;

In my Web.Release.config, which has roughly the same structure as the regular config file, I added the following line:

<pre><span style="color: #339966;">&lt;configuration&gt;
    &lt;add name="EFDbContext" connectionString="Data Source=BEGIJNHOF2\SQLBEGIJNHOF2;
    Initial Catalog=SportStore;Persist Security Info=True;User ID=sporter;Password=somepass;Pooling=False" providerName="System.Data.SqlClient"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/&gt;

You can see the only difference is the xdt:Transform=”SetAttributes”¬†xdt:Locator=”Match(name)” part. This tells the builder that it has to find a node in the Web.config file that has the “EFDbContext” name, and has to replace all the attributes in that node by the ones I specify here, being the new connectionstring.

The only thing you need to do now, before you publish you set your build mode to “release” instead of “debug”, and Vs.Net will do the rest for you!

Nothing special, nothing new, but a handy trick! ūüôā

If there is something unclear, or it’s not working don’t hesitate to contact me or leave a comment! PS: I know the layout sucks big time, but this was a quick post and I’ll get back to it! I promise ūüôā



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s