entity-framework-core - 如何:根据环境配置将提供者特定的 DbContext 选项添加到服务容器
问题描述
我想根据环境配置定位特定的 DbContext 提供程序。(即appsettings.sqlserver.dev.json、appsettings.oracle.dev.json、appsettings.sqlserver.prod.json、appsettings.oracle.prod.json等...)
下面的解决方案有效,但它似乎是一个 hack。此外,EF 似乎忽略了环境配置文件约定,因此您需要为 EF 命令维护默认的 appsettings.json。(添加迁移、删除迁移、更新数据库等...)
是否有任何提供程序环境设置可用于完成此操作?
启动.cs
public void ConfigureServices(IServiceCollection services)
{
string dbProvider = Configuration["Database:Context:Provider"];
string dbConnectionString = Configuration["Database:Context:ConnectionString"];
services.AddDbContext<DatabaseContext>(options =>
{
if (dbProvider == "SqlServer")
{
options.UseSqlServer(dbConnectionString, b => b.MigrationsAssembly("Eagle.Core.Web.Api"));
}
else if (dbProvider == "Oracle")
{
options.UseOracle(dbConnectionString, b => b.MigrationsAssembly("Eagle.Core.Web.Api"));
}
options.EnableSensitiveDataLogging(true);
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
解决方案
推荐阅读
- node.js - 如何使用邮递员一起发送图像/文件和嵌套的 json 数据?
- mongodb - 具有多个匹配条件的 MongoDb 计数列值
- apache-spark-sql - 我如何获得火花数据框的元素?
- performance - 如何避免大部分未使用的条件分支?
- java - 在遍历分配的 LinkedList 时理解 OOP
- javascript - 当我的全局共享服务中的任何内容更新时,为什么我的组件对象不会自行更新?角
- javascript - 如何仅向文本添加边框并对齐中心
- python - 在字符串格式化 MySQL 期间并非所有参数都转换了
- r - 使用 R 将字符串转换为日期
- angular - Visual Studio Code 不会因 Angular 应用程序中未捕获的异常而停止