2
votes

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.

2
Try setting ASPNETCORE_ENVIRONMENT environment variable. I'm not aware of the other way. See: docs.microsoft.com/en-us/aspnet/core/fundamentals/environments I think you should open an issue for that on github so you will get the most satisfying answer.Konrad
Setting that environment is a global environment on a machine level. That could cause issues for other projects deployed to the same machine.Patrick Magee
Perhaps creating an implementation of IDesignTimeDbContextFactory which sets appsettings based on an independent environment variable of your choice? See docs.microsoft.com/en-us/ef/core/miscellaneous/… This ContextFactory would need to be in the project specified by the --startup-project argument when running dotnet efCake or Death
Issue will basically be resolved once this EF Core issue has been completed. github.com/aspnet/EntityFrameworkCore/issues/8332Patrick Magee

2 Answers

2
votes

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.

https://github.com/aspnet/EntityFrameworkCore/issues/8332

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

0
votes

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