4

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

2
  • 1
    launchSettings.json is only used by Visual Studio 2017 when you hit F5 or Ctrl+F5. It's *NOT USED by ASP.NET Core itself. You have to set environment variable in powershell or commandline (cmd.exe)
    – Tseng
    Feb 21, 2018 at 20:34
  • Thanks Tseng you pointed me in the right direction
    – Carntel
    Mar 2, 2018 at 22:08

1 Answer 1

13

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://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell

3
  • It looks this only works for .net core 2.0 (which is OP's case), as mentioned here. Aug 27, 2018 at 10:04
  • 3
    It's not working on VS2019. Environment is not changing at the moment. Apr 3, 2019 at 17:39
  • @user3172616 this is working for me on VS2019, although I did just update so might be something they've fixed. Apr 13, 2019 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.