asp.netweb.config-transform

Web.config connectionString transforms not working when using an external config file


Ok, I am using an external config file for my connection strings so each individual developer can have their own strings while developing. Normally, each dev has a different environment but we all publish to the same servers via web.release.config transforms.

However, when I publish from VS, it's not transforming from the web.release.config for the conn strings. I think it's because if you have the configSource attribute set to use an external config it ignores the transform.

Here's my web.config:

<connectionStrings configSource="userConn.config" /> 

And here's my userConn.config:

<?xml version="1.0"?>
<connectionStrings>  
<add name="DefaultConnection" 
   providerName="System.Data.SqlClient"   
   connectionString="Data Source=XXXX;Initial Catalog=XXXX;user id=XXXX;password=XXXX;" />  
<add name="ExtVariablesEntities"
   providerName="System.Data.EntityClient" connectionString="metadata=res://*/Models.XXXX.csdl|res://*/Models.XXXX.ssdl|res://*/Models.ExtVariables.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=XXXX;initial catalog=XXXX;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework;&quot;" />
</connectionStrings>

After publishing, opening the actual web.config that made it to the server, it still has:

<connectionStrings configSource="userConn.config" />

Is there a workaround for this? I've had this setup before I just don't remember what the trick is.


Solution

  • Just an FYI that I was able to solve this by following this article: https://jshowers.com/simple-web-config-transforms-for-configuration-elements-that-use-configsource-and-external-files/

    You end up with 3 custom config files - whatever you want to call them for dev, test and prod lets say. Then you will have 3 web config files dev, test and prod (these are your web config transform files) where you simply say:

    <appSettings xdt:Transform="Replace" configSource="path.to.custom.config.file.depending.on.env">