EF Core 代码首次迁移和 aspnetcore_environment

EFCore codefirst migrations and the aspnetcore_environment

对于这个问题,我已经使用了一段时间的解决方法,但我想我会在线询问,看看是否有人对我想要实现的目标有任何提示或替代方法。

我有一个非常基本的核心 3.1 MVC 设置,它有这样的设置文件:

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

appsettings.Development.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

appsettings.Local.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=new-test-site;Trusted_Connection=True;"
  }
}

launchsettings.json 中,我有几个配置文件设置如下:

 "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "new_test_site_local": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Local"
      }
    },
    "new_test_site_dev": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }

所以在我看来,当我使用 PMC 设置 $aspnetcore_environment="Local" 时,应该允许我的项目 运行 优先考虑 appsettings.Local.json(例如连接字符串)值。

但它似乎并没有这样做?如果我将“本地”连接字符串复制粘贴到 dev 或“正常”应用程序设置中,我只能让它工作。

如何将我的本地设置作为目标 运行 命令,如 add-migrationupdate-database?!

使用 PowerShell 语法设置环境变量:

$env:aspnetcore_environment="Local"