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?
There's a few ways you could do it:
- Put common configuration settings in machine.config as shown here
- Put common configuration settings in a central file and link to that in each projects's app.config as shown here
- Store the configuration settings in the registry
For me, i always work with the last solution :) Good luck!
You can share the connection strings among multiple projects in a solution as follows:
Create a
ConnectionStrings.config
file with your connection strings under a solution folder, this file should contain only the sectionconnectionStrings
In your projects, add this config file As a Link (add existing item, add as link)
- Select the added file and set its property
Copy to Output Directory
toCopy always
orCopy if newer
- In the
App.config
of your projects, point to the linkedConnectionStrings.config
file using theconfigSource
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>
First, take a look at this post. It describes how you can share the same app.config between multiple projects.
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
来源:https://stackoverflow.com/questions/16154684/centralize-connection-strings-for-multiple-projects-within-the-same-solution