How to configure ConnectionStrings in web.config per developer/user
One thing Martin Fowler states in his article about Evolutionary Database Design is that every developer needs to have its own database instance.
For .NET Windows client projects you can simply change the ConnectionStrings configuration in the App.config file inside the bin directory. But for web/ASP.NET projects this is not possible, because the root directory for debugging the project is the project directory itself (being under source control). I.e. you would have to checkout the web.config file from source control and edit it. This itself is not such a problem, but you always have to watch out to not check the changed web.config file into source control.
A solution to make this work is to split the web.config configuration into multiple files and extend the build process by editing the project file of the web project to create a configuration file which is not in source control and therefore can be customized per developer.
1. Split the configuration
Replace the connectionStrings configuration in your web.config file with the following line:
<connectionStrings configSource="connectionStrings.config" />
2. Create a default configuration
Create a new configuration file “connectionStrings.default.config” with your configured ConnectionStrings (example):
<?xml version="1.0" encoding="utf-8"?> <connectionStrings> <add name="MyDbConnection" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=MyAppDb;User ID=myapp;Password=123pw" /> </connectionStrings>
3. Extend the build process
Edit the project file and include a new target “BeforeBuild” (at the bottom of the file, but inside the “project” node):
<Target Name="BeforeBuild"> <Copy Condition="!Exists('connectionStrings.config')" SourceFiles="connectionStrings.default.config" DestinationFiles="connectionStrings.config" /> </Target>
This extends the build process with a new step which checks whether the file “connectionStrings.config” exists and (if not) copies the file “connectionStrings.default.config” to “connectionStrings.config“. When you want to change a ConnectionString in your local development environment you can simply edit the file “connectionStrings.config” and adjust it to your environment.
4. Add a transformation for publishing
For publishing/deploying your project you can create a web.config transformation which “inlines” the ConnectionStrings again:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings xdt:Transform="Replace"> <add name="MyDbConnection" connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=MyAppDb;User ID=myapp;Password=123pw" /> </connectionStrings> </configuration>
Summing it up, the solution consists of the following parts
- An outsourced ConnectionString configuration not under source control.
- A default ConnectionString configuration under source control.
- A build step ensuring a ConnectionString configuration exists at the expected path.
- (Optional) A web.config transformation for cleaning everything up on publishing/deploying the project.