【问题标题】:how to have two different users to handle EF core migration and CRUD如何让两个不同的用户处理 EF 核心迁移和 CRUD
【发布时间】:2023-03-19 12:57:01
【问题描述】:

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

【问题讨论】:

    标签: docker .net-core entity-framework-core ef-core-3.1


    【解决方案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"));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2018-05-15
      • 2018-05-21
      • 2019-06-18
      • 1970-01-01
      相关资源
      最近更新 更多