C# - Microsoft EntityframeworkCore Factory - 依赖注入

问题描述 投票:0回答:1

我的项目是使用 .NET 8 的 Windows 后台服务。 我想在我当前的项目中使用 Microsoft EntityframeworkCore。 我还想使用

DbContextFactory
在需要的地方创建一个实例。 因此我需要在我的
Program.cs
文件中进行设置。

实际上我需要多解释一下我当前项目的设置。

appsettings.json
中有一个属性值,它控制应使用哪个数据库。这在我的
appsettings.json
中看起来像这样:

...
{
  "AppSettings": {
    "DaysBetweenExecution": 1,
    "ExecutionHour": 3,
    "ExecutionMinute": 0,
    "Environment": "Dev"
},
...

如您所见,环境设置为

Dev
。 所以我在我的
AppSettings
中设置了这个
Program.cs
,如下所示:

...
builder.Services.AddOptions<AppSettings>().BindConfiguration("AppSettings");
...

在我的应用程序中使用这些设置。

AppSettings
类看起来像这样:

namespace BrochureCreationService.ApplicationLogic.Models.AppSettings
{
    public class AppSettings
    {
        public int DaysBetweenExecution { get; set; } = -1; 
        public int ExecutionHour { get; set; } = -1;
        public int ExecutionMinute { get; set; } = -1;
        public string Environment { get; set; } = "Dev";
    }
}

此值

Dev
正在由类
Environment
处理。 这是
Environment
课程:

using Microsoft.Extensions.Options;

namespace BrochureCreationService.ApplicationLogic.Models.Enviroment
{
    public class Environment : IEnvironment
    {
        private IOptions<AppSettings.AppSettings> _AppSettings;

        public Enums.Environment Current { get; set; }

        public Environment(IOptions<AppSettings.AppSettings> appSettings) { 
            _AppSettings = appSettings;
            Current = GetEnvironmentFromAppSettings();
        }

        public Enums.Environment GetEnvironmentFromAppSettings()
        {
            switch(_AppSettings.Value.Environment.ToLower())
            {
                case "dev":
                    return Enums.Environment.Dev;
                case "test":
                    return Enums.Environment.Test;
                case "ittest":
                case "it-test":
                    return Enums.Environment.ITTest;
                case "live":
                case "production":
                    return Enums.Environment.Live;
                default:
                    return Enums.Environment.Dev;
            }
        }

        public string GetConnectionString()
        {
            switch (_Environment.Current)
            {
                case Enums.Environment.Live:
                    return "connection string";
                case Enums.Environment.Test:
                    return "connection string test";
                case Enums.Environment.ITTest:
                    return "connection string it test";
                case Enums.Environment.Dev:
                    return "connection string dev";
                default:
                    return "connection string dev";
            }
        }
    }
}

如您所见,

AppSettings
被注入到构造函数中。该类根据
Environment
中的
appsettings.json
设置为我提供了正确的连接字符串。

现在是我的问题。 我需要在

Program.cs
中设置 EntityframeCore 工厂。 我想这样做:

builder.Services.AddDbContextFactory<IMP_GG_Context>(options =>
{
    var environment = new BrochureCreationService.ApplicationLogic.Models.Enviroment.Environment();
    options.UseSqlServer(environment.GetConnectionString());

}, ServiceLifetime.Scoped);

这里的问题是,这条线不起作用:

var environment = new BrochureCreationService.ApplicationLogic.Models.Enviroment.Environment();

...因为

Environment
在构造函数中需要
AppSettings
或更好的
IOptions<AppSettings.AppSettings>
。我该如何在
Program.cs
中进行管理? 或者有更好的方法吗?非常感谢您的任何提示或帮助。

c# dependency-injection entity-framework-core factory
1个回答
0
投票

首先,我强烈建议您使用应用程序密钥。将

ConnectionString
保留在源代码管理中并不安全。

阅读以下文章以了解此功能的使用: https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets

因此,对于您的 DI 问题,您只需添加以下代码即可:

var options = Configuration.GetSection("AppSettings").Get<AppSettings>();

您可以使用此选项创建

Environment
的新实例。

© www.soinside.com 2019 - 2024. All rights reserved.