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

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

Question

FACTS:

  • net core 2.0 initiative
  • entity framework (code first)
  • various appsettings multiple environments' json files

    My database scripts are created using the Package Manager Console (Add-Migration, Update-Database)

When I execute PM>"Get-DbContext," it retrieves data from my appsettings. GREAT, that's what I want most of the time using the development.json file!

But how do I instruct it to get database variables from appsettings? Instead of development, should PM commands use staging.json?

I've tried making fresh launchSettings.json profiles and setting "ASPNETCORE ENVIRONMENT" to "Staging," but no one appears to take it into consideration except PM.

I can get around that by using Script-Migration to produce scripts, but I like the quick ups and downs I receive and won't utilise that method to deploy to production.

1
3
2/21/2018 7:28:18 PM

Accepted Answer

The ASPNETCORE ENVIRONMENT may be manually updated by performing this command in the Package Manager Console, albeit it is not extensively documented.

PM> $env:ASPNETCORE_ENVIRONMENT='Staging'

then you can use the following command to make sure it is referring to the database you want:

PM> Get-DbContext

This will remove

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

then just execute your instructions as usual. Example:

Update-Database

Commands mentioned: 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