如何更改更新数据库ef迁移的连接字符串?

Cym*_*cus 3 ef-migrations entity-framework-6 dotnet-cli asp.net-core-2.0

我正在尝试运行Update-Database,并且我想指定连接字符串,但是CLI却在寻找错误的字符串。我的appsettings.json文件中有两个连接字符串:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "LocalWindows": "data source=.\\SQLEXPRESS;initial catalog=Intranet;persist security info=True;integrated security=true;",
    "AzureDevelopment": "Server=tcp:en..."
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行Update-Database时,AzureDevelopment始终是它使用的键。因此,如果我将LocalWindows连接字符串复制到AzureDevelopment的值,它将更新正确的数据库。此外,如果我删除AzureDevelopment但像这样离开LocalWindows:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "LocalWindows": "data source=.\\SQLEXPRESS;initial catalog=Intranet;persist security info=True;integrated security=true;"
  }
}
Run Code Online (Sandbox Code Playgroud)

我得到:

Value cannot be null.
Parameter name: connectionString
Run Code Online (Sandbox Code Playgroud)

因此似乎在某个时候,CLI选择使用AzureDevelopment字符串,并且我无法再更改键或提供连接字符串作为参数。

我的问题是,迁移CLI如何知道要使用的连接字符串?它是通过启动设置的反射魔术来检测字符串还是什么?我在网上看到的是在运行Update-Database时如何指定项目。CLI曾经有-ConnectionString和-ConnectionStringName参数,但这些参数不再可用。

Cym*_*cus 5

我要做的就是在PowerShell中执行以下操作:

$env:ASPNETCORE_ENVIRONMENT='LocalWindows' dotnet ef database update

要么

$env:ASPNETCORE_ENVIRONMENT='LocalWindows' Update-Database

启用verbose Update-Database -verbose也会在输出中显示环境,以确保其达到正确的环境。

什么不起作用:

  • 在Windows中设置全局ASPNETCORE_ENVIRONMENT环境变量
  • 在“项目”->“设置”->“调试”屏幕中设置ASPNETCORE_ENVIRONMENT环境变量