Skip to content

Commit 317dda2

Browse files
committed
Set environment variables to "Development" when creating DbContext using IDesignTimeDbContextFactory
Fixes #35174
1 parent b7a436f commit 317dda2

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs

-17
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,6 @@ public virtual IServiceProvider Create(string[] args)
5555
return null;
5656
}
5757

58-
var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
59-
var dotnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
60-
var environment = aspnetCoreEnvironment
61-
?? dotnetEnvironment
62-
?? "Development";
63-
if (aspnetCoreEnvironment == null)
64-
{
65-
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);
66-
}
67-
68-
if (dotnetEnvironment == null)
69-
{
70-
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment);
71-
}
72-
73-
_reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));
74-
7558
try
7659
{
7760
var services = serviceProviderFactory(args);

src/EFCore.Design/Design/Internal/DbContextOperations.cs

+17
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,23 @@ private IDictionary<Type, Func<DbContext>> FindContextTypes(string? name = null,
503503
{
504504
_reporter.WriteVerbose(DesignStrings.FindingContexts);
505505

506+
var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
507+
var dotnetEnvironment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
508+
var environment = aspnetCoreEnvironment
509+
?? dotnetEnvironment
510+
?? "Development";
511+
if (aspnetCoreEnvironment == null)
512+
{
513+
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);
514+
}
515+
516+
if (dotnetEnvironment == null)
517+
{
518+
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment);
519+
}
520+
521+
_reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));
522+
506523
var contexts = new Dictionary<Type, Func<DbContext>?>();
507524

508525
try

test/EFCore.AspNet.InMemory.FunctionalTests/AppServiceProviderFactoryTest.cs

-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ private static void TestCreateServices(Type programType)
2121
var factory = new TestAppServiceProviderFactory(
2222
MockAssembly.Create(programType));
2323

24-
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
25-
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", null);
2624
var services = factory.Create(["arg1"]);
2725

2826
Assert.NotNull(services.GetRequiredService<TestService>());
@@ -66,17 +64,13 @@ public void Create_with_no_builder_method()
6664
[typeof(ProgramWithNoHostBuilder)],
6765
new MockMethodInfo(typeof(ProgramWithNoHostBuilder), InjectHostIntoDiagnostics)));
6866

69-
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
70-
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", null);
7167
var services = factory.Create(["arg1"]);
7268

7369
Assert.NotNull(services.GetRequiredService<TestService>());
7470
}
7571

7672
private static void InjectHostIntoDiagnostics(object[] args)
7773
{
78-
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
79-
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
8074
Assert.Single(args);
8175
Assert.Equal((string[])args[0], new[] { "arg1", "--applicationName", "MockAssembly" });
8276

@@ -91,8 +85,6 @@ private class ProgramWithNoHostBuilder;
9185

9286
private static void ValidateEnvironmentAndArgs(string[] args)
9387
{
94-
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
95-
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
9688
Assert.Equal(args, new[] { "arg1" });
9789
}
9890

test/EFCore.Design.Tests/Design/Internal/DbContextOperationsTest.cs

+4
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ public TestContext()
414414
public TestContext(DbContextOptions<TestContext> options)
415415
: base(options)
416416
{
417+
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
418+
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
417419
}
418420
}
419421

@@ -425,6 +427,8 @@ private TestContextFromFactory()
425427
public TestContextFromFactory(DbContextOptions<TestContextFromFactory> options)
426428
: base(options)
427429
{
430+
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
431+
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
428432
}
429433
}
430434

0 commit comments

Comments
 (0)