• <legend id='fooUd'><style id='fooUd'><dir id='fooUd'><q id='fooUd'></q></dir></style></legend>

    • <bdo id='fooUd'></bdo><ul id='fooUd'></ul>
    <tfoot id='fooUd'></tfoot>

        <small id='fooUd'></small><noframes id='fooUd'>

      1. <i id='fooUd'><tr id='fooUd'><dt id='fooUd'><q id='fooUd'><span id='fooUd'><b id='fooUd'><form id='fooUd'><ins id='fooUd'></ins><ul id='fooUd'></ul><sub id='fooUd'></sub></form><legend id='fooUd'></legend><bdo id='fooUd'><pre id='fooUd'><center id='fooUd'></center></pre></bdo></b><th id='fooUd'></th></span></q></dt></tr></i><div id='fooUd'><tfoot id='fooUd'></tfoot><dl id='fooUd'><fieldset id='fooUd'></fieldset></dl></div>

        使用 IConfigurationRoot 将应用程序的连接字符串传递到 ASP.NET 5 中的存储库类库

        时间:2023-06-08
          <tbody id='zUoww'></tbody>

        <legend id='zUoww'><style id='zUoww'><dir id='zUoww'><q id='zUoww'></q></dir></style></legend>

        <small id='zUoww'></small><noframes id='zUoww'>

          <tfoot id='zUoww'></tfoot>

            • <bdo id='zUoww'></bdo><ul id='zUoww'></ul>
              1. <i id='zUoww'><tr id='zUoww'><dt id='zUoww'><q id='zUoww'><span id='zUoww'><b id='zUoww'><form id='zUoww'><ins id='zUoww'></ins><ul id='zUoww'></ul><sub id='zUoww'></sub></form><legend id='zUoww'></legend><bdo id='zUoww'><pre id='zUoww'><center id='zUoww'></center></pre></bdo></b><th id='zUoww'></th></span></q></dt></tr></i><div id='zUoww'><tfoot id='zUoww'></tfoot><dl id='zUoww'><fieldset id='zUoww'></fieldset></dl></div>

                • 本文介绍了使用 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 应用程序中,如果我想访问任何 appsettings,我可以简单地执行以下操作: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. 想办法将 appsettings 从 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 中的存储库类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Asp.Net 5 (MVC 6) 中使用实体框架 6.x 下一篇:如何包装 Web API 响应(在 .net 核心中)以保持一致性?

                  相关文章

                  <legend id='azIeM'><style id='azIeM'><dir id='azIeM'><q id='azIeM'></q></dir></style></legend>
                    <bdo id='azIeM'></bdo><ul id='azIeM'></ul>

                    1. <tfoot id='azIeM'></tfoot>

                      <small id='azIeM'></small><noframes id='azIeM'>

                      <i id='azIeM'><tr id='azIeM'><dt id='azIeM'><q id='azIeM'><span id='azIeM'><b id='azIeM'><form id='azIeM'><ins id='azIeM'></ins><ul id='azIeM'></ul><sub id='azIeM'></sub></form><legend id='azIeM'></legend><bdo id='azIeM'><pre id='azIeM'><center id='azIeM'></center></pre></bdo></b><th id='azIeM'></th></span></q></dt></tr></i><div id='azIeM'><tfoot id='azIeM'></tfoot><dl id='azIeM'><fieldset id='azIeM'></fieldset></dl></div>