使用 IConfigurationRoot 将应用程序的连接字符串向下传递到 ASP.NET 5 中的存储库类库 [英] Passing application's connection string down to a Repository Class Library in ASP.NET 5 using the IConfigurationRoot

查看:17
本文介绍了使用 IConfigurationRoot 将应用程序的连接字符串向下传递到 ASP.NET 5 中的存储库类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET 5 MVC Web 应用程序,在 Startup.cs 中我看到公共属性

I have an ASP.NET 5 MVC Web Application and in Startup.cs I see that the public property

IConfigurationRoot Configuration 

被设置为builder.Build();

在整个 MVC Web 应用程序中,我可以简单地做

Throughout the MVC Web Application I can simply do

Startup.Configuration["Data:DefaultConnection:ConnectionString"]

appsettings.json 文件中获取 conn 字符串.

to get the conn string from the appsettings.json file.

如何使用构造函数注入将 ASP.NET 5 MVC appsettings.json 中指定的连接字符串传递给我的存储库类库?

How can I get the connection string specified in the ASP.NET 5 MVC appsettings.json passed down to my Repository Class Library using constructor injection?

更新:
这是所有其他存储库继承的基础存储库(如您所见,我现在这里有一个硬编码的连接字符串):

UPDATE:
Here is the base repository that all other repositories inherit from (as you can see I have a hardcoded connection string in here for now):

public class BaseRepo
{
    public static string ConnectionString = "Server=MYSERVER;Database=MYDATABASE;Trusted_Connection=True;";

    public static SqlConnection GetOpenConnection()
    {
        var cs = ConnectionString;
        var connection = new SqlConnection(cs);
        connection.Open();
        return connection;
    }
}

在我的 appsettings.json 文件中的 asp.net 5 web 应用程序中,我有以下内容,相当于将连接字符串添加到 .net 4.5 webapp 中的 web.config:

In my asp.net 5 web application in my appsettings.json file I have the following which is equivalent to adding a connection string to a web.config in a .net 4.5 webapp:

  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=MYSERVER;Database=MYDATABASE;Trusted_Connection=True;"
    }
  }

此外,在我的 asp.net 5 Web 应用程序中,我的 Startup.cs 中有以下默认代码,它将站点配置加载到 IConfigurationRoot 类型的公共属性中:

Additionally in my asp.net 5 web application I have the following default code in my Startup.cs which loads the sites configuration into a public property of type IConfigurationRoot:

 public IConfigurationRoot Configuration { get; set; }
// Class Constructor
        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsDevelopment())
            {
                // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
                builder.AddUserSecrets();
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();
        }

现在在我的 asp.net web 应用程序中,如果我想访问任何应用程序设置,我可以简单地执行以下操作:Startup.Configuration["Data:DefaultConnection:ConnectionString"]

Now in my asp.net web application if I would like to access any of the appsettings I can simple do the following: Startup.Configuration["Data:DefaultConnection:ConnectionString"]

但不幸的是,我无法从我的类库中执行此操作..

But unfortunately I can't do this from my class library..

如果有人想尝试解决这个问题,这里是重现的步骤:

If someone wants to try and figure this out here are the steps to reproduce:

  1. 创建一个新的 ASP.NET 5 MVC Web 应用程序.
  2. 将另一个类库(包)类型的项目添加到项目中.
  3. 找出一种将应用设置从 ASP.NET 5 MVC 应用程序传递到类库的方法

更新后我还是不太明白.这是我的代码:

public class BaseRepo
{
    private readonly IConfigurationRoot config;

    public BaseRepo(IConfigurationRoot config)
    {
        this.config = config;
    }
}

这个类声明不起作用,因为 BaseRepo 现在需要一个构造函数参数.

public class CustomerRepo : BaseRepository, ICustomerRepo
{
    public Customer Find(int id)
    {
        using (var connection = GetOpenConnection())
        {
            ...
        }
    }
}

推荐答案

在您的 Startup.cs 文件中添加以下方法

on your Startup.cs file add the following method

public void ConfigureServices(IServiceCollection services) {
    services.AddSingleton(_ => Configuration);
}

然后像这样更新你的 BaseRepo 类

then update your BaseRepo class like this

public class BaseRepo {
    private readonly IConfiguration config;

    public BaseRepo(IConfiguration config) {
        this.config = config;
    }

    public SqlConnection GetOpenConnection() {
        string cs = config["Data:DefaultConnection:ConnectionString"];
        SqlConnection connection = new SqlConnection(cs);
        connection.Open();
        return connection;
    }
}

这篇关于使用 IConfigurationRoot 将应用程序的连接字符串向下传递到 ASP.NET 5 中的存储库类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆