Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centralize connection strings for multiple projects within the same solution

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?

like image 341
Mithrilhall Avatar asked Apr 22 '13 19:04

Mithrilhall


1 Answers

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....

like image 184
Stacked Avatar answered Sep 21 '22 15:09

Stacked