c# - 在 ASP.NET Core 5 的 update-database 命令中设置 --evironment 参数
问题描述
update-database
在我的 ASP.NET Core 5 WebApi 的包管理器控制台中运行命令时,我试图针对特定环境。
我的项目正在运行EF Core 5.0.5
。
按照EF Core 工具参考链接,我可以成功运行命令
Update-Database -Args '--environment Production'
但是,在我的CreateDbContext
方法中,我仍然得到development
环境变量而不是production
.
public BCASDataContext CreateDbContext(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
// Get environment
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Log.Logger.Information($"Running the DbContext under '{environment}' environment.");
// Build config using main API settings for Database
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Services.WebApi"))
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddEnvironmentVariables()
.Build();
我上面的控制台输出Development
不是Production
预期的?谁能告诉我我做错了什么?
我的目标是能够快速运行迁移,而无需在避免任何问题之前运行单独的环境集,例如意外地针对生产。
解决方案
推荐阅读
- java - 将 Gradle 项目导入 IntelliJ IDEA 时出现问题
- javascript - 页面刷新和页面加载时的随机徽标,但首次访问时始终使用相同的徽标
- html - 如何在 Django HTML 模板中显示包含“\n”的字符串
- javascript - if else 语句中的“反弹”导致警报框弹出多次
- kotlin - 如何从命令行使用增量编译和缓存来编译 kotlin?
- dexie - Dexie,嵌套集合时找不到对象
- flutter - Flutter 小部件测试 Clipboard.setData 未来然后永远不会触发
- java - 什么是 TextFormatter 只允许正双打?
- java - 如何在 Kotlin 中格式化字符串编号以使用逗号?
- flutter - 未处理的异常:键入“列表”
' 不是类型 'Map 的子类型