简体   繁体   English

是否可以在Visual Studio中创建解决方案级别的配置文件?

[英]Is it possible to create a solution level configuration file in Visual Studio?

I have a solution with multiple projects that will use the same connection strings. 我有一个使用相同连接字符串的多个项目的解决方案。 I can add a configuration file to each project with these strings, but was wondering if there was a way to add a single configuration file for the entire solution that could be accessed by all the individual projects within it? 我可以使用这些字符串向每个项目添加配置文件,但是想知道是否有办法为整个解决方案添加单个配置文件,可以由其中的所有单个项目访问?

Instead of adding the config file to each project, use Add as Link so that there is only one copy of the file that all the projects are accessing. 不要将配置文件添加到每个项目,而是使用Add as Link,这样只有一个文件的副本,所有项目都在访问。

Essentially you're adding the address rather than the file itself which means that any changes to the file at the address will affect all projects reading from the address. 基本上你是在添加地址而不是文件本身,这意味着对地址文件的任何更改都会影响从地址读取的所有项目。

Create config once in your first project. 在第一个项目中创建一次配置。

Then all other projects, add the existing item, but hit the arrow on the Add button and Add As Link instead. 然后所有其他项目,添加现有项目,但点击添加按钮上的箭头和添加为链接

The one file will be shared amongst your projects. 这个文件将在您的项目中共享。

I belive you only need the config file in the 'Startup' project. 我相信你只需要'Startup'项目中的配置文件。

I have a solution with multiple projects in that all use the same connection string from a single config file in the startup project. 我有一个包含多个项目的解决方案,它们都使用启动项目中单个配置文件中的相同连接字符串。

alternatively, you could create your own config file which all projects access (not using the .net config file) 或者,您可以创建自己的配置文件,所有项目都可以访问(不使用.net配置文件)

Depending on the projects you have in the solution if you have a MVC/UI project you can use the web.config from that and reference it in other projects within the same solution. 根据解决方案中的项目,如果您有MVC / UI项目,可以使用web.config并在同一解决方案中的其他项目中引用它。

Sample: 样品:

Web.config in MVC project: MVC项目中的Web.config:

<configuration>
...
  <connectionStrings>
    <add name="name1" connectionString="Data Source=HOSTNAME_DB_INSTANCE;Initial Catalog=DB_NAME;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="name2" connectionString="Data Source=HOSTNAME_DB_INSTANCE;Initial Catalog=DB_NAME;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="name3" connectionString="Data Source=HOSTNAME_DB_INSTANCE;Initial Catalog=DB_NAME;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
...
</configuration>

In another project/class library 在另一个项目/类库中

public List<Products> GetProducts()
{
    using (var multi = new SqlConnection(ConfigurationManager.ConnectionStrings["name1"].ConnectionString).QueryMultiple("SPROC NAME", commandType: CommandType.StoredProcedure))
    {
        return multi.Read<Products>().ToList();
    }    
}

This has always worked for me at a solution level. 这在解决方案级别一直对我有用。

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

相关问题 如何在 Visual Studio 中为整个解决方案创建配置文件(json)? - How to create a configuration file (json) for the entire solution in visual studio? 是否可以在Visual Studio 2015中创建一个空的C#解决方案 - Is it possible to create an empty C# solution in Visual Studio 2015 项目级别的Visual Studio解决方案文件夹 - Visual studio solution folder at the project level 如何在visual studio 2015的解决方案打开事件中创建.txt文件? - How to create .txt file on solution opening event of visual studio 2015? 编译Visual Studio解决方案文件 - compiling a visual studio solution file 是否可以将sis项目添加到Visual Studio解决方案? - is to possible to add ssis project to visual studio solution? 是否可以从Microsoft Visual Studio中的sqlite3文件创建dbml? - Is it possible to create dbml from an sqlite3 file in Microsoft Visual Studio? 在Visual Studio 2010中创建Web解决方案安装程序 - To Create a web solution Setup in Visual Studio 2010 创建visual studio解决方案并包括应用程序的设置 - create visual studio solution and include settings of the applicatioon 在Visual Studio项目模板中创建解决方案文件夹 - Create a Solution Folder in a Visual Studio Project Template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM