小编Cym*_*cus的帖子

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

我正在尝试运行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参数,但这些参数不再可用。

ef-migrations entity-framework-6 dotnet-cli asp.net-core-2.0

3
推荐指数
1
解决办法
1572
查看次数