How to remove ConnectionString using Config Transformations

I have a Web.config with multiple ConnectionStrings

<connectionStrings> <add name="connStr1" connectionString="... <add name="ConnStr2" connectionString="... <add name="connStr3" connectionString="... 

Is there a way to use configuration conversions to remove a specific connection string? Something like:

 <connectionStrings> <xdt:Remove connStr2? 

Obviously there is no next to the correct syntax, but you get my drift ...

+51
c # visual-studio web-config web-config-transform
Jan 19 '12 at 2:50
source share
2 answers

From the MSDN documentation on the topic:

 <configuration xmlns:xdt="..."> <connectionStrings> <add xdt:Transform="Remove" /> </connectionStrings> </configuration> 

Transform="Remove" is the magic you are looking for. There is also Transform="RemoveAll" , which you could use in conjunction with specific add- Transform="RemoveAll" .

EDIT

Alternatively, you can also combine the Locator attribute with the above Remove to limit which items you want to remove.

More specifically:

 <configuration xmlns:xdt="..."> <connectionStrings> <add xdt:Transform="Remove" xdt:Locator="XPath(configuration/connectionStrings[@name='ConnStr2'])" /> </connectionStrings> </configuration> 

Or something like this should work.

+52
Jan 19 '12 at 3:14
source share

This will delete a specific connection string based on its name.

 <configuration> <connectionStrings> <add name="ConnStr2" xdt:Transform="Remove" xdt:Locator="Match(name)" connectionString=" " /> </connectionStrings> </configuration> 

Note that the value of connectionString not an empty string, but a space. Any non-empty value will do.

+83
Jan 30 '12 at 11:45
source share



All Articles