It is a fairly common scenario that during development the local machine configuration settings for different developers in are not same and it may not match with the default value in source control.
One such example can be SQL server connection string. Some developers may have SQL Express installed, others may have a named instance on their local. However, the
app.config can only have one value for this setting.
The way people (at least “I” used to) handle this usually is to change the
configvalues on the local machine during development but discard those changes at the time of commit. If accidentally, however you forget to change the setting back to original, then there is a risk of breaking the build or code on other developer’s local machine.
Override your appSettings
Recently, my colleague (thanks James!) showed me a nice little trick handle this. I must say, I was bit embarrassed to not know this already.
You can override the key-value pairs defined under
appSettings element of
config by using the
|<add key="dbConnectionString" value="sourceControlConnectionString"/>|
|<add key="someRandomApplicationSetting" value="sourceControlApplicationSetting"/>|
localAppSettings.config is not part of your source control. If your local settings match exactly as
web.config,then you do not need to have this file on your local. Else, you can use this file to override only the settings that are different than
|<add key="dbConnectionString" value="localMachineConnectionString"/>|
When you use the application settings in your code, you get following values:
|Key –> Value|
Important Note: If you use
connectionStringselement to store your data source connection string, then, you can use configSource attribute instead. The two attributes however, are not equivalent. You can read more about the difference here.
Hope this nice little trick helps ease your development. 🙂