Share ConnectionStrings between projects for each developer differently
Share ConnectionStrings
A customer of mine have a Visual Studio C# solution with more than one project which uses the same ConnectionString. So how can you define your ConnectionString(s) once and share them between projects?
This is how my sample solution looks like. The “Console” and “ServerConsole” projects should use the same ConnectionStrings.
I created a XML file called “connections.config” in the solution root folder and added it to the solution via the “Add Existing Item” dialog. This file contains all my ConnectionStrings I need.
Content of connections.config:
1: <?xml version="1.0" encoding="utf-8"?>
2: <connectionStrings>
3: <add name="DefaultDatabase"
4: providerName="System.Data.SqlClient"
5: connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=ShareConnection; Integrated Security=True" />
6: </connectionStrings>
After that, I add the file to all projects, which are needing the same connections. You need to add the file as “Add As Link” to ensure using the file in the shared location.
Now you see the file in your project with a little arrow on the left bottom (linked file).
To ensure the file is copied to the build output directory, you need to modify the “Copy to Output Directory” setting of the linked file to “Copy Always”.
Now write the following “connectionStrings” section in the “App.config” of your projects:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <appSettings>
4: <add key="SpecificProjSetting" value="SomeValue" />
5: </appSettings>
6: <connectionStrings configSource="connections.config" />
7: </configuration>
And that’s all. If you now write your code, you always get the shared ConnectionString e.g.:
1: public string GetDbConnection()
2: {
3: return ConfigurationManager.ConnectionStrings["DefaultDatabase"].ConnectionString;
4: }
Individual ConnectionStrings for each developer
My second requirements, enabling each developer to have his own ConnectionStrings (they have different Instance-Names), needs only a small addition.
In addition to the “connections.config” file which is under Source Control, each developer can create his own file at the same place with the convention “connections.config.{COMPUTERNAME}” which will not be checked in. In my example my computer’s name is “NIM-50″, so the file is named as “connections.config.NIM-50″.
Edit now each project’s (Pre-Build/Post/Build) property in the “Build Events” tab with the following command lines.
Pre-build event command line:
if exist $(TargetDir)connections.config del $(TargetDir)connections.config
Post-build event command line:
if exist $(SolutionDir)connections.config.$(COMPUTERNAME) copy $(SolutionDir)connections.config.$(COMPUTERNAME) $(TargetDir)connections.config
The “Pre-Build” checks if a “connections.config” allready exists in the drop location and deletes it. If the “Pre-Build” wouldn’t be run, then the old (existing) file will not be overwritten!
The “Post-Build” checks if a specific developer configuration is found and copy/rename it to the drop location. If no one exists, the standard shared config will be used as defined in the project’s include.
The described solution is also working for unit tests but you need to edit the *.testrunconfig file. Because the connections.config isn’t copied to the out folder of the TestResults structure you need to include it into your deployment.
Open the *.testrunconfig by double-clicking in solution explorer and go to the “Deployment” tab. Now you can add the connections.config as additonal file to deploy.
Originally posted by Salomon Moyal:
Dear Nicolas,
The aforementioned workaround is great and works quite well with the Sql 2005 and a none service-packed Visual Studio 2008.
Another issue came up during the deployment.
Our software is a client-server architecured solution. As a result the client makes no direct connection to the DAL.
The server-side service is tightly connected to the DAL Tier. The Visual studio vs 2008 server installer is writing the content of the app.config into the ServerProject.config forgettingt the connectionstring.config.
You need to go to the installer project and add it manually (right-click: add existing Item move to the solution folder files and link it).
The installer will copy it on the target directory.
I still got problems writing a clean installer for the database. The databse project itself too is not yet so clean. Conceptually the database installer has no real connection to the Database project.
Signed: A customer of mine
Sincerly
Salomon