Reputation: 7935
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>
Upvotes: 4
Views: 3945
Reputation: 5186
It's because you don't have an xdt:Transform attribute.
You have a couple of options, but in this case, you'd most likely have to use in your Web.Debug.config and Web.Release.config something like:
<connectionStrings>
<add name="MyContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDb.mdf" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
the xdt:Transform attribute tells it that the tag is already there on the original Web.config file and that it just needs to replace all the attributes with the ones you provide, and the xdt:Locator tells it which setting attribute to identify the tag by. In this case, by connection string Name.
Since transforms only execute when you DEPLOY (not when you run the application), this means that you have to use xdt:Transform, because you still want the connection string to be there on the original web.config, and only transform/replace its attributes when deployed.
In some other transformation cases, you can use xdt:Transform=Insert, which means that a setting/entry is not on the original web.config and the transformation will insert it when deployed only.
Upvotes: 3
Reputation: 14880
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
Upvotes: 14