dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.63k stars 3.15k forks source link

configuration name containing - (minus) are not set well as environment variable #33922

Open mihaimarin83 opened 3 months ago

mihaimarin83 commented 3 months ago

I have a configuration that has the name "Development-Test". I used this name since some years and never had problems until porting from ef6 to efcore.

The problem is that the environment variable contains the configuration name until the - (minus) sign.

configuration Development-Test => environment Development

Add-Migration Test C:\Program Files\dotnet\dotnet.exe exec --depsfile C:.....\WebUI.Core\bin\Debug\net8.0\WebUI.Core.deps.json --additionalprobingpath C:\Users....nuget\packages --runtimeconfig C:.....\WebUI.Core\bin\Debug\net8.0\WebUI.Core.runtimeconfig.json C:\Users....nuget\packages\microsoft.entityframeworkcore.tools\8.0.6\tools\netcoreapp2.0\any\ef.dll migrations add test --json --verbose --no-color --prefix-output --assembly C:.....\WebUI.Core\bin\Debug\net8.0\WriteService.DataAccess.dll --project C:......WriteService.DataAccess\WriteService.DataAccess.csproj --startup-assembly C:.....\WebUI.Core\bin\Debug\net8.0\WebUI.Core.dll --startup-project C:.....\WebUI.Core\WebUI.Core.csproj --project-dir C:......WriteService.DataAccess\ --language C# --configuration Development-Test --working-dir C:\Repositories...\src --root-namespace WriteService.DataAccess

Using assembly 'WriteService.DataAccess'. Using startup assembly 'WebUI.Core'. Using application base 'C:\Repositories...\src\WebUI.Core\bin\Debug\net8.0'. Using working directory 'C:\Repositories...\src\WebUI.Core'. Using root namespace 'WriteService.DataAccess'. Using project directory 'C:\Repositories...\src\WriteService.DataAccess\'. Remaining arguments: . Finding DbContext classes... Finding IDesignTimeDbContextFactory implementations... Finding application service provider in assembly 'WebUI.Core'... Finding Microsoft.Extensions.Hosting service provider... Using environment 'Development'. Using application service provider from Microsoft.Extensions.Hosting. Finding DbContext classes in the project... Found DbContext 'WriteModel'. Using DbContext factory 'MigrationsContextFactory'.

As you can see, we send "--configuration Development-Test " but is taken into account only 'Development'.

This is very unplesant because we need to read the connection string from the specific "appsetting.Development-Test.json" file in the DesignTimeDbContextFactory like:

`public class MigrationsContextFactory : IDesignTimeDbContextFactory { public WriteModel CreateDbContext(string[] args) { //ASPNETCORE_ENVIRONMENT is 'Development' instead of 'Development-Test' var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{environmentName}.json")
                .Build();

            ..........
    }

}`

Thanks.

roji commented 3 months ago

Have you tried enclosing the value in quotes, i.e. --configuration "Development-Test"?

mihaimarin83 commented 3 months ago

Have you tried enclosing the value in quotes, i.e. --configuration "Development-Test"?

This is not under my control. I just execute "Add-Migration Test" from the Package Manager Console