Net Core 2 - 实体框架:不同环境的更新数据库 [英] Net Core 2 - Entity Framework: Update-Database of different environment
问题描述
事实:
- net core 2.0 项目
- 实体框架(代码优先)
针对不同环境的不同appsettings.json文件
- net core 2.0 project
- entity framework (code first)
different appsettings.json file for different environments
我使用包管理器控制台来生成我的数据库脚本(添加迁移、更新数据库)
I utilize Package Manager Console to generate my DB scripts (Add-Migration, Update-Database)
如果我运行 PM>Get-DbContext",它会带回从我的 appsettings.Development.json 文件中提取的信息,这就是我大部分时间想要的!
If I run PM>"Get-DbContext" it brings back info pulled from my appsettings.Development.json file GREAT, that's what I want most of the time!
但我如何告诉它从 appsettings.Staging.json 中提取 db 变量而不是为 PM 命令开发?
我尝试创建新的 launchSettings.json 配置文件并设置 "ASPNETCORE_ENVIRONMENT": "Staging" 但除了 PM 之外,一切似乎都尊重这一点.
I tried creating new launchSettings.json profiles and setting "ASPNETCORE_ENVIRONMENT": "Staging" but everything seems to respect that except PM.
PS 解决它以使用 Script-Migration 生成脚本,但我想要快速 UP 和 DOWN 我得到并且不会使用它来部署到 prod
推荐答案
没有很好的文档记录,但您可能必须通过在包管理器控制台中运行此命令来手动更改 ASPNETCORE_ENVIRONMENT
Not well documented but you can have to changed the ASPNETCORE_ENVIRONMENT manually by running this command in Package Manager Console
PM> $env:ASPNETCORE_ENVIRONMENT='Staging'
然后你可以运行这个命令来验证它是否指向你想要的数据库:
then you can run this command to verify it is pointing to your desired database:
PM> Get-DbContext
这会踢出去
providerName databaseName dataSource options
------------ ------------ ---------- -------
Microsoft.EntityFrameworkCore.SqlServer myDatabase tcp:fake.database.windows.net,1433 None
然后正常运行您的命令.示例:
then just run your commands as normal. Example:
Update-Database
对命令的引用:https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
这篇关于Net Core 2 - 实体框架:不同环境的更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!