【问题标题】:How to get settings from appsettings.json in a referenced .NET Framework project如何从引用的 .NET Framework 项目中的 appsettings.json 获取设置
【发布时间】:2016-12-07 14:55:28
【问题描述】:

我有一个在 .NET Framework 4.6.1 中运行的 EntityFramework 6 作为项目的 DAL。在其他 .NET Framework 项目的解决方案中运行良好。现在我要使用引用到我的 ASP.NET Core WebApi 的相同 DAL 项目(使用 .NET Framework 4.6.1),但它不起作用。异常说:

在应用程序配置文件中找不到名为“MyEntities”的连接字符串。

.NET Core 项目没有 web.config 文件,所以 EF6 如何将我的 Core 项目中的 appsettings.json 中的设置传递给引用的 DAL 项目?

我找到了一些让 EF6 在 .NET Core 项目中运行的示例(例如 https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6),但这对我没有帮助。我想这是因为 dbcontext 是在 DAL 项目中创建的。

appsettings.json

"ConnectionStrings": {
  "MyEntities": "<my connectionString>"
},

项目.json

"dependencies": {
  "EntityFramework": "6.1.3",
  ...
},
...
"frameworks": {
  "net461": {
    "dependencies": {
      "DAL": {
        "target": "project"
      }
    },
    "imports": "dnxcore50"
  } 
},

【问题讨论】:

    标签: entity-framework-6 .net-core


    【解决方案1】:

    嗯...我没有得到我想要的答案。所以我做了一个解决方法,我不得不更改 DAL 项目中的一些代码。希望它不会破坏其他项目的某些功能...

    在 DAL 项目中:
    由于我首先使用带有模型的 EF 实体,因此无法更改模型。但是它创建了部分类,所以我为 MyEntity 做了另一个部分类,它带有一个带参数的构造函数。

    namespace DAL.Models
    {
        public partial class MyEntities
        {
            public MyEntities(string connString) : base(connString)
            {
            }
        }
    }
    

    在 DAL 存储库中,我创建了一个带有参数的构造函数(用于从我的 .NET 核心项目注入)和一个私有方法。

    public class UserRepository : IUserRepository
    {
        private readonly string _dbContextString;
    
        public UserRepository(string dbContextString)
        {
            _dbContextString = dbContextString;
        }
    
        private MyEntities GetMyEntities()
        {
            return string.IsNullOrEmpty(_dbContextString) ? new MyEntities() : new MyEntities(_dbContextString);
        }
        ...
    }
    

    然后我搜索并替换了 MyEntity() 的用法,来自

        using (var context = new MyEntities())
        {
            ...
    

        using (var context = GetMyEntities())
        {
            ...
    

    在我的 .NET 核心项目中:
    在 Startup.cs 中,我在 ConfigureService 中添加了一行以将 connectionString 注入我的存储库中。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    
        services.AddSingleton<IUserRepository>(new UserRepository(Configuration.GetConnectionString("MyEntities")));
        ...
    

    Tada,现在它可以与我的新 .NET 核心项目一起使用,并且仍然可以与其他 .NET 框架项目一起使用!

    【讨论】:

      【解决方案2】:

      您必须在所需的控制器中注入 IConfiguration 并将其传递给 Dal 项目

       //dependency inject it in required controller or service class
          IConfiguration Config 
       //this will get your connection string 
          Config.GetSection("ConnectionStrings").GetSection("MyEntities")
      

      【讨论】:

      • 我不确定如何将它放入我的控制器中。我应该如何初始化Config,以及如何将其传递给 DAL 项目?
      • 我找到了一个answer,我可以用它来将连接字符串传递给项目。但这不是我的解决方案,因为 DAL 被其他项目使用,我无法改变处理来自他们的调用的方式。我希望我可以让我的 appsettings.json 可用于解决方案中的 .net 项目。
      • 让您的 DAL 项目接受连接字符串作为静态属性或构造函数参数。然后从将使用 DAL 项目的项目中传递连接字符串。
      • 我最终得到了一个像您的想法一样的解决方案,将连接字符串从核心注入到 dal,感谢您的想法。我确实希望我可以在不更改 dal 项目的情况下做到这一点,但我必须这样做。但我没有使用IConfiguration,因为 Startup.cs 处理像Configuration.GetConnectionString("MyEntities") 这样的连接字符串 :)
      猜你喜欢
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多