views:

1573

answers:

4

I need to change my connection string in the web.config file based on an environment variable (for different enviornments, like dev/staging/production, etc). I have seen other solutions that use build tasks to accomplish changing different configurations, but haven't been able to find something that will let me change my connection string based on an environment variable. Does anyone know of any way to do this?

+2  A: 

How about having two connection strings and another variable, like "isTesting" in your web.config, then based on the value of isTesting pick which connection string to use?

Cory Larson
A: 

you can also use config sections, and based upon server name switch between sections. this way you can have keys named the same.

link text

N8
+4  A: 

We make use of the configSource attribute for the appSettings and connectionStrings elements in the web.config.

Basically, we have the same web.config file for all of our environments: dev, qa and production.

Then we utilize seperate "environment specific" files.. For example...

In web.config:

<?xml version="1.0"?>
<configuration>
  <appSettings configSource="local.appsettings.config" />
  <connectionStrings configSource="local.connectionstrings.config" />
</configuration>

Then we maintain the following files:

local.appsettings.config.development
local.appsettings.config.qa
local.appsettings.config.production
local.connectionstrings.config.development
local.connectionstrings.config.qa
local.connectionstrings.config.production

Since we pre-compile all of our asp.net applications before deployment, we've got a custom msBuild task utilized by our CI solution that copies the right configuration files (based on the target environment) to the proper .config file...

So, if we are deploying to dev, local.appsettings.config.development -> local.appsettings.config

If we are deploying to qa, local.appsettings.config.qa -> local.appsettings.config

This allows us to keep the core web.config the same across all of our environments.

datacop
Hey thanks, is there any way to do this based on an environment variable though? I would like to do it this way but my client asked to do it based on an environment variable they have setup in each of their different environments. If it does do that, I am missing that part (where it looks at an environment variable). Thanks
Ryan
You would have to keep the connection strings for each environment in a single .config file.. something like:<connectionStrings> <add name="dbString_dev" connectionString="" /> <add name="dbString_qa" connectionString="" /> <add name="dbString_prod" connectionString="" /></connectionStrings>Then, in your code (assuming you have some sort of a "configuration object" class then you can read in the right string based on the environment variable. The *MAJOR* flaw with that plan is.. you now have all of your production configuration info in the same file as your dev and qa.
datacop
I use this methode with configSource files in the App_Data folder. You can publish your site without the App_Data folder, mixing it later with the configuration of your server environment.
Dirk
A: 

You can set a web.config for each environment in the configuration manager using prebuild events. I have tried this with excellent results.

http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

When you have debug and build you can have local/preproduction/production... etc

Pablo Castilla