Skip to content

Commit

Permalink
[release/9.0] Set environment variables to "Development" when creatin…
Browse files Browse the repository at this point in the history
…g DbContext using IDesignTimeDbContextFactory (#35230)

Fixes #35174
  • Loading branch information
AndriySvyryd authored Dec 2, 2024
1 parent cc53f41 commit 017e8dc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
17 changes: 0 additions & 17 deletions src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,23 @@ private IDictionary<Type, Func<DbContext>> 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<Type, Func<DbContext>?>();

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestService>());
Expand Down Expand Up @@ -66,17 +64,13 @@ 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<TestService>());
}

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" });

Expand All @@ -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" });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ public TestContext()
public TestContext(DbContextOptions<TestContext> options)
: base(options)
{
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
}
}

Expand All @@ -425,6 +427,8 @@ private TestContextFromFactory()
public TestContextFromFactory(DbContextOptions<TestContextFromFactory> options)
: base(options)
{
Assert.Equal("Development", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
Assert.Equal("Development", Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"));
}
}

Expand Down

0 comments on commit 017e8dc

Please sign in to comment.