Different connection string for each publish profile in VS2010
Asked Answered
H

1

5

Is it possible to change connection string (or just server host) relying on selected web publish profile? Maybe using Web.config transform or someway else?

I mean for profile "Test" change connection string "MyConnString" (in published Web.config) to "Data Source='example.com,14333;..." and for profile "Production" - to "Data Source=./SQLExpress;..."

Hoover answered 17/6, 2010 at 20:45 Comment(0)
U
14

This is exactly what web config transforms were created for. The link you provided in your post has a walkthrough of doing this specifically for connection strings.

To start with the transforms, right-click your web.config file in the project explorer and choose "Add Config Transforms". Assuming that you have ConfigA and ConfigB in your solution configuration, there will be two new files added, Web.ConfigA.config and Web.ConfigB.config.

If you open these new files, they'll be pretty empty except for a bunch of comments. They actually contain a connection string example in them that you can use though - it looks like this:

<connectionStrings>
  <add name="MyDB" 
    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Uncomment this section, and change the "name" property to the name of the connection string in the base web.config file. Set the "connectionString" property to the actual value that you want to use for ConfigA. So, like this:

<connectionStrings>
  <add name="myConnectionString" 
    connectionString="Data Source=ConfigASqlServer;Initial Catalog=ConfigADatabase;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

Repeat the process for the Web.ConfigB.config file, with the desired connection string for ConfigB.

Now when you use the Publish command in visual studio, it will automatically transform the base web.config file, and set the "connectionString" attribute to whatever configuration you're in when you publish.

Upbeat answered 17/6, 2010 at 21:17 Comment(5)
I really can't figure out how to use Web.config transform. Could you please suggest a brief example how to change connection string to CS_A for profile A and to CS_B for B?Hoover
As far as I could understated, SetAttribute replaces whole attribute value. Is it possible to replace just a part of string being attribute value?Hoover
And another question, please. Transform depends on configuration - Debug, Release, etc. Is it possible ti link it with publishing profiles - Server1, Server2, etc. I use the same configuration for all my profiles. Or I have to create a separate configuration for each profile? But how link profile and configuration in this case?Hoover
abatishchev, in Web.Release.config, Web.Debug.config you may want to update things like compilcation in debug mode, errors and logging and in Web.Staging.config, Web.Release.config you may have connection strings and stuff like that. See samples: github.com/kriasoft/site-sdkStores
This explains how to change Web.config based on build configuration. However the question was how to do this based on publishing profile, not configuration.Phonemics

© 2022 - 2024 — McMap. All rights reserved.