user1365911
user1365911

Reputation:

Removing specific connection strings in web.config transformation

I have a few connection strings in my web.config that I want to remove using web.config transformation when I deploy my web app. I had found the MSDN page talking about web.config transformation but it only list a "Remove" which removes the first entry or the "RemoveAll" that deletes them all. Is there a way to only remove specific connection strings?

  <connectionStrings>
<add name="DB"
     connectionString="Data Source=; Initial Catalog=; User ID=; Password=;"
     providerName="System.Data.SqlClient" />
<add name="ErrorLog"
     connectionString="Data Source=; Initial Catalog=; User ID=; Password=;"
     providerName="System.Data.SqlClient" />
<add name="SiteFiles"
     connectionString="" />
<add name="FanFiles"
     connectionString="" />

Upvotes: 3

Views: 4104

Answers (1)

rsbarro
rsbarro

Reputation: 27339

To remove specific connection strings by name, you can use the following format:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="ErrorLog" xdt:Transform="Remove" xdt:Locator="Match(name)" />
    <add name="SiteFiles" xdt:Transform="Remove" xdt:Locator="Match(name)" />
  </connectionStrings>

</configuration>

The transform xml above will remove the ErrorLog and SiteFiles connection strings. It uses the xdt:Locator attribute to match on the name attribute.

Upvotes: 5

Related Questions