Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swap Connection String when publishing a project?

I can see that Web.config file contains two files:

-web.Debug.config
-web.Release.config

Inside this config files there is the following comment:

In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an atrribute "name" that has a value of "MyDB".

<connectionStrings>
  <add name="MyDB" 
    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

What is the "Match" locator ? I already have a connection String in Web.config So how do I set this up? should the main web.config file contain the production connection string or the other way around? I'm looking for adivices from people that have done similar things

like image 786
meda Avatar asked Mar 22 '23 02:03

meda


1 Answers

We are using xdt:Transform="Replace" which basically replaces the connection string of our development DB and it works perfectly. See below:

Development connection string (in your case web.Debug.config):

<connectionStrings>
  <add name="MyDB" connectionString="Data Source=DebugSQLServer;Initial Catalog=MyDebugDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Production connection string (in your case web.Release.config):

<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="Replace" xdt:Locator="Match(name)" />
</connectionStrings>
like image 171
gregjer Avatar answered Apr 11 '23 05:04

gregjer