编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

如何更改更新数据库ef迁移的连接字符串?:How do I change connection string for Update-Database ef migrations?

chausner 数据库/缓存 2022-5-11 10:23 8人围观

腾讯云服务器
如何更改更新数据库ef迁移的连接字符串?的处理方法

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

I am trying to run Update-Database, and I would like to specify the connection string, but the CLI is looking at the wrong one. There are two connection strings in my appsettings.json file:

{ "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:

When I run Update-Database, AzureDevelopment is always the key it uses. So if I copy the LocalWindows connectionstring to AzureDevelopment's value, it updates the correct database. Furthermore, if I delete AzureDevelopment but leave LocalWindows like this:

{ "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字符串,并且我无法再更改键或提供连接字符串作为参数.

So it seems at some point, the CLI chose to use the AzureDevelopment string, and I can no longer change the key or supply the connection string as an argument.

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

My question is how does the migrations CLI know what connection string to use? Is it detecting the string through some reflection magic on the startup settings or what? All I see online is how to specify the project when running Update-Database. There used to be -ConnectionString and -ConnectionStringName parameters for the CLI, but those are no longer available.

问题解答

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

All I had to do was the following in PowerShell:

$ env:ASPNETCORE_ENVIRONMENT ='LocalWindows'dotnet ef数据库更新

$ env:ASPNETCORE_ENVIRONMENT ='LocalWindows'更新数据库

使用 Update-Database -verbose 启用详细信息还会在输出中显示环境,以确保其达到正确的环境.

Turning on verbose with Update-Database -verbose also shows you the environment in the output to make sure it's hitting the correct one.

什么不起作用:

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

这篇关于如何更改更新数据库ef迁移的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^