Table of Contents

IT:AD:ConnectionStrings:HowTo:Strategies To Share Among Developers

Summary

Whereas a lot of settings can be moved from AppSettings to the Database, the ConnectionString to the Database cannot (as it is needed to connect to the database).

Strategies

The question is not new, and several solutions have been tried:

The solution* I preferred is:

Note:that I said preferred in the past tense. That was before I discovered NConfig.

#

### Use an External ConnectionString.config

Edit the config file to configSource in an external config file fragment:

<configuration>
  <connectionStrings configSource="DEV\connectionStrings.config"/>
</configuration>

Note that the config file in turn points has to be have a root node named `connectionStrings` (matching the name of the node the `configSource` attribute was attached to in the original config file)

The fragment file's contents would look like:

<connectionStrings>
  <add name="AppDbContext" 
    providerName="System.Data.ProviderName" 
    connectionString="Valid Connection String;" />
</connectionStrings>

Each Developer keeps his own connectionStrings.config file and ensures it is never checked in.

TODO: Explain how to lock it from Checking in.

Use Transforms to Cleanup before Releasing to ST, QAT, PROD

Cleaning up the config file before releases to ST and PROD is handled by XDT Transforms (see: IT:AD:XDT:HowTo:Transform Connection Strings)

For example, the XDT for Web.ST.config could look like one of the following solutions:

   <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <connectionStrings xdt:Transform="SetAttributes" configSource="ST\ConnectionStrings.config"/>
    </configuration>

Alternatively, replace the whole thing with an inline connection string:

   <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
 
  <connectionStrings xdt:Transform="Replace">
    <add name="AppDbContext" connectionString="todo" providerName="todo" />
  </connectionStrings>

    </configuration>

ACiD/ of this approach:

Using the File Attribute for Individual AppSettings

The above configSource element works for whole config file sections – such as connectionStrings – but does not work for single AppSettings, which is needed for handling unique values per machine (an example being a PostBack url that would have to registered in an primitive SSO/ solution (SAML based SSO's would expect the return address to be posted in the first request).

It at first appears there is no solution for settings such as those…that is incorrect.

Use the AppSettings File attribute for such scenarios.

  <appSettings file="Config\Dev\appSettings.config">
    <add key="KeyA" value="someBaseValueA"/>
    <add key="KeyB" value="someBaseValueB"/>
  </appSettings>

Which points to Configs\Dev\appSettings.config (again, a file that each dev does not check in):

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="KeyB" value="I'm the replacement value per developer"/>
</appSettings>

Notice that KeyB will override any default value that is in the base config.

Notice also that if the file is missing, KeyB will fall back to the value that is in the base config file.

You can clean this up with the following XTD:

  <appSettings xdt:Transform="RemoveAttributes"/>

Resources

* http://msdn.microsoft.com/en-us/library/aa903313(v=vs.71).aspx * http://msdn.microsoft.com/en-us/library/dd465326.aspx * http://www.codeproject.com/Articles/399002/Project-Build-Web-config-transformation