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 have been changed and parameter -Environment have been removed. in 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 on all kind of ways but when i run the update-database
with ef core 2.0 it don´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 with updating diffrent environments?
If I start the application with diffrent lanchsettings it works but not using the package manager console.
Using the package manager in Visual Studio was a dead end for me. The solution was:
Add below in .csproj in the starter project in solution:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
Open the command tool(cmd) and go the the same folder as .csproj for start project are located(Default project).
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
I ran into this same issue when I upgraded everything to core 2.0. @bricelam response works for me (even though I prefer the older way) and I posted this same issue and response here: github.com/aspnet/EntityFrameworkCore/issues/9664
Trying to do this for "Development" environment. I've hit the same problem but I can't get it to work with the above solution. I have appsettings.json , and appsettings.development.json (each with a different db connection string). The above approach doesn't work, either in cmd or package manager. Config is picked up correctly in both environments in the app itself. Would love any insight!
Whats happens is that ef read the environment from the setting ASPNETCORE_ENVIRONMENT. Before you run the update-database in cmd check what env your pointing on with "echo %ASPNETCORE_ENVIRONMENT%" . And as deepinsight check your startup.cs and the row. .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
And to update the the database automaticly you add in your configure/startup. DbInitializer.Initialize(_context) and in that method you add _context.Database.MigrateAsync() . And the migraion applies on the environment when you startup the env.