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

asp.net-core ef-migrations entity-framework-6

Question

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
3
2/21/2018 7:28:18 PM

Accepted Answer

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

8
3/2/2018 10:05:48 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow