Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/9.0] Set environment variables to "Development" when creating DbContext using IDesignTimeDbContextFactory #35230

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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