在没有EF的情况下在appsettings.json中获取多个连接字符串 [英] Get Multiple Connection Strings in appsettings.json without EF
问题描述
通过迁移我开发的当前MVC .Net应用程序开始使用.Net Core RC2.在我看来,是因为使用appsettings.json处理配置的方式是:如果我有多个连接字符串,则要么必须使用EF来检索连接字符串,要么必须为每个连接字符串创建单独的类.我看到的所有示例都使用EF(对我而言这没有意义,因为我将使用Dapper),或者该示例将构建一个以config中的部分命名的类.我是否缺少更好的解决方案?
Just starting playing with the .Net Core RC2 by migrating a current MVC .Net app I developed. It looks like to me because of the way that configuration is handled with appsettings.json that if I have multiple connection strings I either have to use EF to retrieve a connectionstring or I have to create separate classes named for each connection string. All the examples I see either use EF (which doesn't make sense for me since I will be using Dapper) or the example builds a class named after the section in the config. Am I missing a better solution?
"Data": {
"Server1": {
"ConnectionString": "data source={server1};initial catalog=master;integrated security=True;"
},
"Server2": {
"ConnectionString": "data source={server2};initial catalog=master;integrated security=True;"
}
}
为什么我要建立两个类,如果每个类的唯一属性是一个连接字符串,那么一个类就命名为"Server1"?
Why would I want to build two classes, one named "Server1" and another "Server2" if the only property each had was a connectionstring?
推荐答案
我对Adem对使用RC2的响应进行了一些更正,所以我认为最好将它们发布.
There are a couple of corrections that I made to Adem's response to work with RC2, so I figured I better post them.
我配置了appsettings.json并创建了一个类似于Adem的类
I configured the appsettings.json and created a class like Adem's
{
"ConnectionStrings": {
"DefaultConnectionString": "Default",
"CustomConnectionString": "Custom"
}
}
和
public class ConnectionStrings
{
public string DefaultConnectionString { get; set; }
public string CustomConnectionString { get; set; }
}
Adem的大多数代码都是VS for RC2的开箱即用,因此我只是将以下行添加到ConfigureServices方法中
most of Adem's code comes out of the box in VS for RC2, so I just added the line below to the ConfigureServices method
services.Configure<Models.ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
主要缺失点是必须将连接字符串传递给控制器(一旦您指定了强类型的配置对象并将其添加到服务集合中,就可以请求它通过请求IOptions实例 https:来从任何Controller或Action方法访问://docs.asp.net/en/latest/mvc/controllers/dependency-injection.html )
The main missing point is that the connection string has to be passed to the controller (Once you’ve specified a strongly-typed configuration object and added it to the services collection, you can request it from any Controller or Action method by requesting an instance of IOptions, https://docs.asp.net/en/latest/mvc/controllers/dependency-injection.html)
这要交给控制器了,
private readonly ConnectionStrings _connectionStrings;
public HomeController(IOptions<ConnectionStrings> connectionStrings)
{
_connectionStrings = connectionStrings.Value;
}
,然后在实例化DAL时传递适当的connectionString
and then when you instantiate the DAL you pass the appropriate connectionString
DAL.DataMethods dm = new DAL.DataMethods(_connectionStrings.CustomConnectionString);
所有示例都说明了这一点,但他们没有说明,为什么我直接从DAL中提取的尝试没有用
All the examples show this, they just don't state it, why my attempts to pull directly from the DAL didn't work
这篇关于在没有EF的情况下在appsettings.json中获取多个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!