Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I publish, the content of connectionStrings in Web.Release.config is not copied

I have a connectionString in Web.Debug.config, and another one, different, in Web.Release.config.

When I publish my project, the content of Web.Release.config does not appear in the Web.config published. Why?

Web.config

<configuration>
    <connectionStrings>
        <!-- <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-SalvaVidas-20130610104655;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-SalvaVidas-20130610104655.mdf" /> -->
    </connectionStrings>

Web.Debug.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <connectionStrings>
        <add name="MyContext"
             providerName="System.Data.SqlClient"
             connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDb.mdf" />
    </connectionStrings>

Web.Release.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <connectionStrings>
        <add name="MyContext"
             providerName="System.Data.SqlClient"
             connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDb;Server=ServerName;Database=MyDb;Trusted_Connection=True;Integrated Security=SSPI;" />
    </connectionStrings>
like image 230
Fabricio Avatar asked Dec 03 '22 22:12

Fabricio


1 Answers

The problem is that you are not using Web.debug.config / Web.release.config as transformation.

You need to do:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyContext"
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SalvaVidas;Server=ServerName;Database=MyDb;Trusted_Connection=True;Integrated Security=SSPI;" 
         xdt:Transform="SetAttributes"
         xdt:Locator="Match(name)"/>
</connectionStrings>

The xdt:Locator entry will find an entry in your original Web.config by the name attribute, located in connectionString > add. The xdt:Transform will change the attributes to the one specified in your Web.release/debug.config.

However, this will not work in the current state of your Web.config because the connection string entry is commented out. If you leave it commented, you need to change the xdt:Transform to Insert.

For more information on Web.config transforms, look at this MSDN entry

like image 59
Simon Belanger Avatar answered Jan 12 '23 20:01

Simon Belanger