91

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://learn.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.

jamheadart
  • 5,047
  • 4
  • 32
  • 63
joakimja
  • 2,707
  • 2
  • 17
  • 25
  • try `set ASPNETCORE_ENVIRONMENT=EnvironmentName` before executing the command. – Anton Toshik Aug 29 '17 at 12:54
  • 2
    Are you using a PowerShell prompt? You'll need to use `$env:ASPNETCORE_ENVIRONMENT='EnvironmentName'` – bricelam Aug 30 '17 at 16:08
  • 1
    I tried it, I set ASPNETCORE_ENVIRONMENT=Production in the PM console in visual studio but it don´t use the environment it just use the default development. – joakimja Aug 31 '17 at 12:15
  • Note that in the accepted answer of $env:ASPNETCORE_ENVIRONMENT = "Development" the **$env** portion is case sensitive. $Env:ASPNETCORE_ENVIRONMENT will work in some situations but IDesignTimeDbContextFactory will not pick it up. Scratched my head for awhile on this... – ElectroMagnetic Hobbyist Mar 28 '20 at 16:09

6 Answers6

185

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.

Martin Florin
  • 1,851
  • 1
  • 7
  • 8
  • 9
    It worked! Just curious: do I need to reset it to development or will it be automatically reset on each build? each VS startup? – David Liang May 01 '18 at 20:53
  • 9
    This should be a bit more upvoted and probably added to the documentation. – Rafael Herscovici Oct 11 '18 at 14:00
  • 7
    @DavidLiang It resets on each VS Startup – Digvijayad Dec 07 '18 at 00:36
  • 3
    My appsettings.json point to a different database. However my appsettings.Development.json points to the correct location. using: $env:ASPNETCORE_ENVIRONMENT='Development' and then: update-database doesn't work. it still looks at the incorrect db string. https://github.com/aspnet/EntityFrameworkCore/issues/7353#issuecomment-393471579 – Sniipe May 07 '19 at 11:23
  • I Tried to upvote twice!! ^^ I'm here again... Thank you Martin! – Igor Quirino Oct 06 '20 at 16:50
  • Is there a way to remove the variable (without restarting VS)? – Vočko Nov 26 '20 at 05:16
  • 1
    @Vočko You can use the command: ```Remove-Item env:ASPNETCORE_ENVIRONMENT``` – Martin Florin Nov 30 '20 at 09:55
  • This has worked for me on many projects, but a recent one I'm working on it does not seem to take effect until after hammering the command multiple times. I've lost count of the amount of times I've set environment to dev and it's updated staging, then I run it again and it does dev. Frustrating. – jamheadart Sep 30 '21 at 14:28
  • do we also need to set the env variables in production before running the migrations on a production database? – karthik suryadevara Jul 31 '23 at 17:21
31

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

brunochaina
  • 719
  • 7
  • 7
  • 4
    does not seem to set then ENV in the context of `CreateDbContext` when trying to leverage the `IDesignTimeDbContextFactory` method. any thoughts here? – workabyte Sep 06 '21 at 19:03
  • 4
    Running in powershell yields error: `unrecognized option '--'`. Does anyone know how to solve? I tried `--%`, didn't work neither. – Maroun Jan 03 '22 at 18:41
  • I also got the same error as @Maroun. Unrecognized option '--' How do I fix this? – HamsterWithPitchfork May 31 '22 at 14:10
  • This does not work. if you do `Console.WriteLine( Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))` it will still report `Development` – fullStackChris Feb 01 '23 at 08:41
  • You need to use `Update-Database -Args '--environment Production'`. Source: https://stackoverflow.com/a/65206291/19112855 – Codingwiz May 11 '23 at 17:43
11

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.

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
Hardy Hobeck
  • 218
  • 3
  • 6
  • Hi, I removed the text saying that you were unable to post a comment since statements like that should not be part of an answer. Also, it didn't apply here as you _did_ post an answer and not a comment ;) – Modus Tollens Jul 09 '20 at 12:24
3

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
The Mighty Chris
  • 1,578
  • 13
  • 17
  • @AlexeyStrakh For me, the problem was that the Connection-String had a different name in the development-environment – Marius Steinbach Apr 28 '19 at 20:28
  • 3
    According to [the official documentation](https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#aspnet-core-environment), the --environment option specifies the build environment, not the configuration environment. – carlin.scott Nov 13 '19 at 00:35
  • 2
    The solution to use environment in the ef cli is no longer valid and ef core 2.0 onwards now respects environment settings ref : [https://github.com/dotnet/efcore/issues/15026#issuecomment-472967827] – Ram Jan 15 '20 at 03:40
  • 2
    I guess I'm a little late, but, .net 5 still uses the --environment command, you just have to add a second pair of hyphens prior to that. "-- --environment Production" the first -- will tell the CLI to interpret the rest as arguments. – Morilon Jan 18 '21 at 00:24
  • 2
    Running in powershell doesn't work: `unrecognized option '--'` – Maroun Jan 03 '22 at 18:44
  • You need to use `Update-Database -Args '--environment Production'`. Source: https://stackoverflow.com/a/65206291/19112855 – Codingwiz May 11 '23 at 17:43
2

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

$env:ASPNETCORE_ENVIRONMENT = 'Development'
dotnet ef migrations add newMigrationTest
ScareCrow
  • 497
  • 3
  • 6
1

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

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
joakimja
  • 2,707
  • 2
  • 17
  • 25
  • 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: https://github.com/aspnet/EntityFrameworkCore/issues/9664 – Los Morales Sep 01 '17 at 14:06
  • 1
    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! – BenjiFB Jan 07 '18 at 01:02
  • 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) – joakimja Jan 08 '18 at 07:00
  • 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. – joakimja Jan 08 '18 at 07:08