Net Core 2 - Entity Framework: Update-Database of different environment

余生颓废 提交于 2020-01-01 03:36:10

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/48913968/net-core-2-entity-framework-update-database-of-different-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!