EF Core Tool Ignoring the launchSettings.json in .net core 2.1 and 3.1

Viewed 737

In launchSettings.json, some variable are defined and application using these variables. When I am running application normally, then environment variables available into the launchSettings.json successfully loaded and app running perfectly. But when I am trying to add migrations, then ef tool ignoring the launchSettings.json file.

launchSettings.json looks like

{ "MIN_LOG_LEVEL" : "Warn" }

Code to read launchSettings.json variables as

config.AddEnvironmentVariables();
string logLevel = config["MIN_LOG_LEVEL"] // returing null

Some snapshots of code

Program.cs enter image description here

launchSettings.json enter image description here

Update: I tried with both IIS and Kestrel Server, but in both cases this issue is occurring.

1 Answers

After searching a lot regarding this issue, I figure out one thing that really helps me.

The current available EF Core tools does not support for the launchSettings.json automatically and If we want to get the variables present inside the launchSettings.json, then we manually need to parse the file.

The CLI added support for launchSettings.json but till now it works only with dotnet run command. EF Core tools does not have the facility to get the launchSettings.json variable automatically.

This feature will be added soon as I got to know from the github issue

if you want to take advantage of the launchSettings.json then you can do it as below.

IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Properties", "launchSettings.json"), optional: false, reloadOnChange: true)
            .Build();
Related