From 317dda24664593c2891b2f1d5d26e4ab8798e6b0 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Wed, 27 Nov 2024 16:24:06 -0800 Subject: [PATCH] Set environment variables to "Development" when creating DbContext using IDesignTimeDbContextFactory Fixes #35174 --- .../Internal/AppServiceProviderFactory.cs | 17 ----------------- .../Design/Internal/DbContextOperations.cs | 17 +++++++++++++++++ .../AppServiceProviderFactoryTest.cs | 8 -------- .../Design/Internal/DbContextOperationsTest.cs | 4 ++++ 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs b/src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs index 2015337b4e6..b797f8a4add 100644 --- a/src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs +++ b/src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs @@ -55,23 +55,6 @@ public virtual IServiceProvider Create(string[] args) return null; } - var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); - var dotnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"); - var environment = aspnetCoreEnvironment - ?? dotnetEnvironment - ?? "Development"; - if (aspnetCoreEnvironment == null) - { - Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment); - } - - if (dotnetEnvironment == null) - { - Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment); - } - - _reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment)); - try { var services = serviceProviderFactory(args); diff --git a/src/EFCore.Design/Design/Internal/DbContextOperations.cs b/src/EFCore.Design/Design/Internal/DbContextOperations.cs index da44b15b283..761579c8bdb 100644 --- a/src/EFCore.Design/Design/Internal/DbContextOperations.cs +++ b/src/EFCore.Design/Design/Internal/DbContextOperations.cs @@ -503,6 +503,23 @@ private IDictionary> FindContextTypes(string? name = null, { _reporter.WriteVerbose(DesignStrings.FindingContexts); + var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); + var dotnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"); + var environment = aspnetCoreEnvironment + ?? dotnetEnvironment + ?? "Development"; + if (aspnetCoreEnvironment == null) + { + Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment); + } + + if (dotnetEnvironment == null) + { + Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment); + } + + _reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment)); + var contexts = new Dictionary?>(); try diff --git a/test/EFCore.AspNet.InMemory.FunctionalTests/AppServiceProviderFactoryTest.cs b/test/EFCore.AspNet.InMemory.FunctionalTests/AppServiceProviderFactoryTest.cs index 84b3f4c2c46..02bf4c65e98 100644 --- a/test/EFCore.AspNet.InMemory.FunctionalTests/AppServiceProviderFactoryTest.cs +++ b/test/EFCore.AspNet.InMemory.FunctionalTests/AppServiceProviderFactoryTest.cs @@ -21,8 +21,6 @@ private static void TestCreateServices(Type programType) var factory = new TestAppServiceProviderFactory( MockAssembly.Create(programType)); - Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null); - Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", null); var services = factory.Create(["arg1"]); Assert.NotNull(services.GetRequiredService()); @@ -66,8 +64,6 @@ public void Create_with_no_builder_method() [typeof(ProgramWithNoHostBuilder)], new MockMethodInfo(typeof(ProgramWithNoHostBuilder), InjectHostIntoDiagnostics))); - Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null); - Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", null); var services = factory.Create(["arg1"]); Assert.NotNull(services.GetRequiredService()); @@ -75,8 +71,6 @@ public void Create_with_no_builder_method() private static void InjectHostIntoDiagnostics(object[] args) { - Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); - Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")); Assert.Single(args); Assert.Equal((string[])args[0], new[] { "arg1", "--applicationName", "MockAssembly" }); @@ -91,8 +85,6 @@ private class ProgramWithNoHostBuilder; private static void ValidateEnvironmentAndArgs(string[] args) { - Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); - Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")); Assert.Equal(args, new[] { "arg1" }); } diff --git a/test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs b/test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs index 990345659b5..4120e9ad05f 100644 --- a/test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs +++ b/test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs @@ -414,6 +414,8 @@ public TestContext() public TestContext(DbContextOptions options) : base(options) { + Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); + Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")); } } @@ -425,6 +427,8 @@ private TestContextFromFactory() public TestContextFromFactory(DbContextOptions options) : base(options) { + Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); + Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")); } }