How to run `dotnet ef` commands to target a specific appsettings.[environment].json in EF Core 2.0 ef-core-2.0 entity-framework-core


The docs mention that there is no support in 2.0 for passing in custom environment variable, but EF core 1.0 supported the --environment argument. I kind of feel that this feature should still be supported in EF Core 2.0.

Currently, I am deploying multiple applications to the same machine, but different versions on the same machine. I am using Puppet to automate some of the deployment process and end up with websites in IIS such as Company.Project.[env]. Where env is something like .dev, .test, .uat each running under a seperate AppPool etc. Now, with ASP.NET Core 2.0 I can apply an <environmentVariable> to the web.config for each deployed aspnet core website and that environment variable in the web.config takes priority over any machine environment variable of ASPNETCORE_ENVIRONMENT. During puppet deployment it inserts the correct value in the web.config for the ASPNETCORE_ENVIRONMENT variable, this makes perfect sense to me and allows each deployed app to still be bootstrapped correctly and load in the correct appsettings.[environment].json file.

Now, with EF Core 2.0, I cannot do this. I was planning on deploying a separate database project for each environment and suffixing each deployed package with the environment name just like how I am doing so with the websites and was planning on somehow passing in an --environment to run any dotnet ef commands and make them read the correct appsettings.[environment].json file which would have the correctly targeted database environment connection string to run migrations against.

If ASPNET Core websites support an <environmentVariable> in the web.config which allows for multiple projects to be deployed to the same machine consuming unique environment variables. Then I feel like EF Core should support passing in --environment.

Since this is no longer supported, what would be the best way to deploy an application multiple times on the same machine but also run dotnet ef commands to target a specific appsettings.[environment].json to ensure the ef command uses a specific connection string in the deployed project and then run the migrations on the correct database on that machine.

10/5/2017 10:23:45 AM

Accepted Answer

EF Core will support a way of passing in custom arguments to the ef tooling commands. There is an issue I found that is tracking this feature.

Once this is done, then connection strings or any other environment argument can then be passed along with ef commands.

10/11/2017 4:32:00 PM

Popular Answer

Following the advice from @Konrad:

on terminal running on your Project path type

export ASPNETCORE_ENVIRONMENT=Production <- or your environment

after that any command that you run it will use the appsettings.json of your environment (appsettings.Production.json)

I'm currently using asp net core 2.2

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow