Net Core 2-实体框架:不同环境的更新数据库 [英] Net Core 2 - Entity Framework: Update-Database of different environment

查看:155
本文介绍了Net Core 2-实体框架:不同环境的更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事实:

  • net core 2.0项目
  • 实体框架(代码优先)
  • 适用于不同环境的不同appsettings.json文件

    我利用Package Manager控制台生成数据库脚本(添加迁移,更新数据库)

如果我运行PM>"Get-DbContext",它将带回从我的appsettings.Development.json文件GREAT中提取的信息,这是我大多数时候想要的!

但是我如何告诉它从appsettings.Staging.json中提取数据库变量,而不是为PM命令进行开发?

我尝试创建新的launchSettings.json配置文件并设置"ASPNETCORE_ENVIRONMENT":暂存",但除PM以外,其他所有内容似乎都尊重这一点.

PS可以解决该问题,以通过Script-Migration生成脚本,但是我希望获得快速的UP和DOWN,并且不会使用它来部署到产品中

解决方案

文档尚不完善,但是您必须通过在程序包管理器控制台中运行此命令来手动更改ASPNETCORE_ENVIRONMENT.

PM> $env:ASPNETCORE_ENVIRONMENT='Staging'

然后您可以运行以下命令来验证它是否指向所需的数据库:

PM> Get-DbContext

将会踢出

providerName                            databaseName          dataSource                                 options
------------                            ------------          ----------                                 -------
Microsoft.EntityFrameworkCore.SqlServer myDatabase            tcp:fake.database.windows.net,1433           None

然后只需正常运行命令即可.示例:

Update-Database

对命令的引用: https://docs.microsoft. com/en-us/ef/core/miscellaneous/cli/powershell

FACTS:

  • 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)

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!

But how do I tell it to pull db variables from appsettings.Staging.json instead of development for PM commands?

I tried creating new launchSettings.json profiles and setting "ASPNETCORE_ENVIRONMENT": "Staging" but everything seems to respect that except PM.

PS work around it to generate script with Script-Migration but I would like the fast UP and DOWN I get and wont use it to deploy to prod

解决方案

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

which will kick out

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

Reference to commands: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell

这篇关于Net Core 2-实体框架:不同环境的更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆