18

I currently have three projects in my solution that all have their own App.config file with the same exact connection string.

Is there a way to consolidate the connections strings / app.config files so that I only need to make changes to one location?

Mithrilhall
  • 1,485
  • 8
  • 33
  • 52
  • 1
    possible duplicate of [Share a Connection string between multiple web projects](http://stackoverflow.com/questions/1660502/share-a-connection-string-between-multiple-web-projects) – pedrommuller Apr 26 '14 at 19:47

3 Answers3

24

You can share the connection strings among multiple projects in a solution as follows:

  1. Create a ConnectionStrings.config file with your connection strings under a solution folder, this file should contain only the section connectionStrings

  2. In your projects, add this config file As a Link (add existing item, add as link)

  3. Select the added file and set its property Copy to Output Directory to Copy always or Copy if newer
  4. In the App.config of your projects, point to the linked ConnectionStrings.config file using the configSource attribute: <connectionStrings configSource="ConnectionStrings.config" />

ConnectionStrings.config

<connectionStrings>
    <add name="myConnStr" connectionString="Data Source=(local); Initial Catalog=MyDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    ...
    <connectionStrings configSource="ConnectionStrings.config" />
    ...
</configuration>

Read more details....

Stacked
  • 6,892
  • 7
  • 57
  • 73
  • 1
    You can't figure it out until you give it a try ;) don't wait for others to test code for you. – Stacked Jun 14 '17 at 11:25
  • 2
    Great answer, you may need to add 'bin\' prefix if are you copying to output directory. – Andrew M. Oct 17 '17 at 23:27
  • And what if I need two separate connection strings for debug and release (like Web.Debug.config and Web.Debug.Release)? – JohnB Jul 27 '18 at 07:33
  • Very good way to handle this. Thanks, @Stacked. I've got a question similar to the one from JohnB's: can you perform transformations on this shared configuration file? – Alex Dec 28 '18 at 12:09
  • Much better than just linking to 404 pages with no context or discussion. Works great! – Phillip Copley Mar 21 '19 at 19:25
  • @JohnB I guess you would have ConnectionStrings.Debug.config and ConnectionStrings.Release.config that you would link to in each of your projects and reference in the Web.Debug.config or Web.Release.config or App.Debug.config or App.Release.config. It's a lot of file duplication though :/ – br3nt Oct 10 '19 at 01:29
4

There's a few ways you could do it:

  1. Put common configuration settings in machine.config as shown here
  2. Put common configuration settings in a central file and link to that in each projects's app.config as shown here
  3. Store the configuration settings in the registry

For me, i always work with the last solution :) Good luck!

Obama
  • 2,586
  • 2
  • 30
  • 49
3

First, take a look at this post. It describes how you can share the same app.config between multiple projects.

How to Share App.config?

Second, take a look at one other post, which describes how you let different app.config-files have a reference to one single shared xml-file which contains the connection strings.

Use XML includes or config references in app.config to include other config files' settings

Community
  • 1
  • 1
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54