首页 > 解决方案 > 如何让两个不同的用户处理 EF 核心迁移和 CRUD

问题描述

我正在寻找一种解决方案来处理两个不同的 SQL 用户,超级用户执行 EF 核心迁移和低权限用户来处理应用程序 CRUD。但是,解决方案是将应用程序 dockerize 并且 SQL 用户应该能够作为环境变量传递。

标签: docker.net-coreentity-framework-coreef-core-3.1

解决方案


您可以尝试覆盖 DbContext 中的 OnConfiguring 方法,并且可以根据某些条件设置不同的连接字符串(不同的用户)。

.AddDbContext 扩展方法将 DbContext 注册为范围服务,因此您应该能够处理 DbContext 的每个实例化的目的。

我会试着给你一个想法:

public class ApplicationDbContext : IdentityDbContext
{
    private readonly IConfiguration configuration;
    public ApplicationDbContext(IConfiguration configuration)
    {
        this.configuration = configuration;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        bool someCondition = true;
        builder.UseSqlServer(configuration.GetConnectionString(someCondition ? "SuperUserConnectionString" : "CrudConnectionString"));
    }
}

推荐阅读