ef core doesn't use ASPNETCORE_ENVIRONMENT during update-database

C#.Net CoreVisual Studio-2017Entity Framework-Core

C# Problem Overview


I use visual studio to update all my environments with a certain migration. It had worked fine using the command below.

update-database -Migration initMigrationProduct -c ProductContext -Environment Production

In ef core 2.0 this command has been changed and parameter -Environment has been removed. In the docs it said.

> "With 2.0, you can use the ASPNETCORE_ENVIRONMENT environment variable > instead."

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

I have now tried all kinds of ways but when I run the update-database with ef core 2.0 it doesn't use the ASPNETCORE_ENVIRONMENT variable. I tried to set in registry, application properties.

Please let me know what I need to do to get this working to update different environments?

If I start the application with different launchsettings it works but not using the package manager console.

C# Solutions


Solution 1 - C#

To set the ASPNETCORE_ENVIRONMENT variable in Package Manager Console (PMC) , inside visual studio, to Production use this command first

$env:ASPNETCORE_ENVIRONMENT='Production'

Then you can use

Update-Database

normally.

Solution 2 - C#

Starting in EF Core 5.0, the environment can also be provided using the --environment argument, but you also need to specify a '--' token before:

dotnet ef database update -- --environment Production

As the documentation states:

> The -- token directs dotnet ef to treat everything that follows as an > argument and not try to parse them as options. Any extra arguments not > used by dotnet ef are forwarded to the app

Solution 3 - C#

I had the same problem like the reporter of this issue and tried out the recent solution in the Package Manager Console (PMC) and set the environment variable with the command:

$env:ASPNETCORE_ENVIRONMENT='YOUR_ENVIRONMENT'

Unfortunetly I have a ConsoleApplication and use the generic host builder on startup with Host.CreateDefaultBuilder(). In this scenario the environment variable prefix is not ASPNETCORE_ but DOTNET_.

For me this command works in PMC:

$env:DOTNET_ENVIRONMENT='YOUR_ENVIRONMENT'

I want to thank Martin Florin and others to guide me in the correct direction.

Solution 4 - C#

According to EntityFrameworkCore#6846 the correct solution is to use the --environment option, the dotnet ef commands do not respect ASPNETCORE_ENVIRONMENT

dotnet ef database update --environment Production

Solution 5 - C#

Using the package manager in Visual Studio was a dead end for me. The solution was:

  1. Add below in .csproj in the starter project in solution:

     <ItemGroup>
         <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
     </ItemGroup>
    
  2. Open the command tool(cmd) and go the the same folder as .csproj for start project are located(Default project).

  3. Run the command as Anton Toshik suggested set ASPNETCORE_ENVIRONMENT=Production

4.Then run the command dotnet ef database update initMigrationProduct -c ProductContext And now it works.

REMARK: in this command database and update have changed place since earlier versions. And there are no argument/code for migration. The docs explain more after this clarification:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

Solution 6 - C#

You can use the following in the command line/terminal for setting the environment variable.

$env:ASPNETCORE_ENVIRONMENT = 'Development'
dotnet ef migrations add newMigrationTest

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoakimjaView Question on Stackoverflow
Solution 1 - C#Martin FlorinView Answer on Stackoverflow
Solution 2 - C#brunochainaView Answer on Stackoverflow
Solution 3 - C#Hardy HobeckView Answer on Stackoverflow
Solution 4 - C#The Mighty ChrisView Answer on Stackoverflow
Solution 5 - C#joakimjaView Answer on Stackoverflow
Solution 6 - C#ScareCrowView Answer on Stackoverflow