首页 > 解决方案 > 在 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预期的?谁能告诉我我做错了什么?

我的目标是能够快速运行迁移,而无需在避免任何问题之前运行单独的环境集,例如意外地针对生产。

标签: c#entity-frameworkentity-framework-coreasp.net-core-webapi

解决方案


推荐阅读