Ef core does not use ASPNETCORE_ENVIRONMENT during database update

I use visual studio to update all my environments with a specific migration. It worked perfectly using the command below.

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

In ef core 2.0, this command has been changed and the -Environment option removed. in the documents he 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 tried all kinds of ways now, but when I run update-database with ef core 2.0, it does not use the ASPNETCORE_ENVIRONMENT variable. I tried to set application properties in the registry.

Please let me know what I need to do for this to work with updating different environments?

If I run the application with different lanch settings, it works, but does not use the package manager console.

+25
c # visual-studio-2017 .net-core entity-framework-core
source share
4 answers

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

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

     <ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup> 
  2. Open a command tool (cmd) and go to the same folder as .csproj to start the project (Project by default).

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

4. Then run the dotnet ef database update initMigrationProduct -c ProductContext and now everything works.

NOTE : in this command, database and update have been swapped from earlier versions. And there are no arguments / code to migrate. The docs explain more after this clarification:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

+1
source share

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

 $env:ASPNETCORE_ENVIRONMENT='Production' 

Then you can use

 Update-Database 

fine.

+38
source share

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

 dotnet ef database update --environment Production 
+2
source share

Recently I had a problem with an EF tool that does not use the connection strings from my appsettings.Production file, it turns out that if the EF tool has problems with appsettings.Production (for example, a null line), it will return to using application settings , I was not able to see that this was written to the console using the EF tool, but when using the -verbose flag when updating the database, the environment will be written to the console. I just would like to mention this, since I have not seen this documented anywhere.

0
source share

All Articles