【发布时间】:2023-03-19 12:57:01
【问题描述】:
我正在寻找一种解决方案来处理两个不同的 SQL 用户,即执行 EF 核心迁移的超级用户和处理应用程序 CRUD 的低权限用户。但是,解决方案是将应用程序 docker 化,并且 SQL 用户应该能够作为环境变量传递。
【问题讨论】:
标签: docker .net-core entity-framework-core ef-core-3.1
我正在寻找一种解决方案来处理两个不同的 SQL 用户,即执行 EF 核心迁移的超级用户和处理应用程序 CRUD 的低权限用户。但是,解决方案是将应用程序 docker 化,并且 SQL 用户应该能够作为环境变量传递。
【问题讨论】:
标签: docker .net-core entity-framework-core ef-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"));
}
}
【讨论】: