views:

322

answers:

1

I have recently decided to move my connection strings to machine.config as this seems to be by far the most elegant approach for managing multiple environments. However, I would still like to be able to override these settings in my local web.config if the need arises (or the non-enlightened masses begin to complain).

How can I override settings from machine.config in my web.config without getting a ConfigurationErrorsException because the value has already been set?

+2  A: 

If you're trying to add another connection string using the local web.config that has the same name as one you've added in machine.config, you will need to remove it first. The connectionStrings element works like a dictionary, you can add a remove or clear tag before adding the replacement string. Take a look at this for details.

Rory
Ok, thank makes sense to me. Basically they are merged as one global configuration to the application, so what I was trying to do is actually not possible.
jcm
If what you were trying to do is not possible, then I must have misunderstood the question. If memory serves, SQL Server Express creates a connection string in machine.config called "Local Server" or something, which is then available to every application because, as you've said, the configs appear merged at runtime. To override SQL's connection string, you can add a <remove name="Local Server" /> tag to your web.config followed by <add name="Local Server" ... /> with the new configuration.
Rory
Yea I misunderstood. It helps when you read the docs ;) I get it now. Thank you, this is perfect.
jcm