如何更改Update-Database ef迁移的连接字符串?

时间:2018-01-24 21:38:29

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

我正在尝试运行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..."
  }
}

当我运行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;"
  }
}

我明白了:

Value cannot be null.
Parameter name: connectionString

所以在某些时候,CLI似乎选择使用AzureDevelopment字符串,我不能再更改密钥或提供连接字符串作为参数。

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

2 个答案:

答案 0 :(得分:4)

我所要做的只是PowerShell中的以下内容:

import numpy as np;np.random.seed(1) import pandas as pd import matplotlib.pyplot as plt import seaborn as sns a = np.concatenate((np.random.binomial(3,0.3,50)*2.2+1, np.random.rayleigh(3,50))) df = pd.DataFrame({"height" : a, "feature" : ["A"]*50+["B"]*50}) fig = plt.figure() fig.add_subplot(1, 2, 1) ax = sns.violinplot(x='feature', y='height', data=df, scale='count', hue='feature', split=True, palette='seismic', inner='quartile') fig.add_subplot(1, 2, 2) ax = sns.violinplot(x=["AB"]*len(df), y='height', data=df, scale='count', hue='feature', split=True, palette='seismic', inner='quartile') plt.tight_layout() plt.show()

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

使用$env:ASPNETCORE_ENVIRONMENT='LocalWindows' Update-Database打开详细信息还会向您显示输出中的环境,以确保它能够正确输出。

什么行不通:

  • 在Windows中设置全局ASPNETCORE_ENVIRONMENT环境变量
  • 在Project-> Settings-> Debug屏幕中设置ASPNETCORE_ENVIRONMENT环境变量

答案 1 :(得分:0)

在 EF Core 5.0 中,可以像这样指定连接字符串:

dotnet ef database update --connection "<connection string>"