docker - 如何让两个不同的用户处理 EF 核心迁移和 CRUD
问题描述
我正在寻找一种解决方案来处理两个不同的 SQL 用户,超级用户执行 EF 核心迁移和低权限用户来处理应用程序 CRUD。但是,解决方案是将应用程序 dockerize 并且 SQL 用户应该能够作为环境变量传递。
解决方案
您可以尝试覆盖 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"));
}
}
推荐阅读
- python - Kivy 如何在弹出窗口中正确使用 FileBrowser
- javascript - 迭代 knockout.js 可观察对象的 javascript 属性以创建订阅
- ruby-on-rails - 在 rspec 测试运行之前清除 sprockets 缓存
- visual-studio-code - 如何以编程方式在 VS Code 中显示 CompletionList?
- c++ - 不同范围内的 C++ 对象引用
- php - PHP shmop 重新加载时的段错误
- c# - VS2010 C# 在目标中不包括正确的 .net 框架版本
- polymer-2.x - 聚合物 2 嵌套 ShadowRoots
- ios - UILabel numberOfLines 随动画变化
- bixby - Bixby:如何根据我的位置获取信息?比如天气等