简体   繁体   English

集中同一解决方案中多个项目的连接字符串

[英]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. 目前,我的解决方案中有三个项目,每个项目都有自己的App.config文件,具有相同的确切连接字符串。

Is there a way to consolidate the connections strings / app.config files so that I only need to make changes to one location? 有没有一种方法可以合并连接字符串/ app.config文件,这样我只需要对一个位置进行更改?

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 使用解决方案文件夹下的连接字符串创建一个ConnectionStrings.config文件,该文件应仅包含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 选择添加的文件并将其属性“ Copy to Output DirectoryCopy always Copy if newerCopy 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" /> 在项目的App.config中,使用configSource属性指向链接的ConnectionStrings.config文件: <connectionStrings configSource="ConnectionStrings.config" />

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

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

Read more details... . 阅读更多详细信息...。

There's a few ways you could do it: 有几种方法可以做到:

  1. Put common configuration settings in machine.config as shown here 把常用的配置设置在machine.config中所示, 在这里
  2. Put common configuration settings in a central file and link to that in each projects's app.config as shown here 把常用的配置设置在中央文件和链接到每个项目的app.config中所示位置
  3. Store the configuration settings in the registry 将配置设置存储在注册表中

For me, i always work with the last solution :) Good luck! 对我来说,我总是使用最后一种解决方案:)祝您好运!

First, take a look at this post. 首先,看看这篇文章。 It describes how you can share the same app.config between multiple projects. 它描述了如何在多个项目之间共享相同的app.config。

How to Share App.config? 如何共享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. 其次,看看另一篇文章,它描述了如何让不同的app.config文件引用一个包含连接字符串的单个共享xml文件。

Use XML includes or config references in app.config to include other config files' settings 在app.config中使用XML包含或配置引用来包含其他配置文件的设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM