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

Allow the last DbContext to be registered to use nongeneric options #32569

Merged
merged 1 commit into from
Dec 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ private static void AddCoreServices<TContextImplementation>(
CreateDbContextOptions<TContextImplementation>,
optionsLifetime));

serviceCollection.TryAdd(
serviceCollection.Add(
new ServiceDescriptor(
typeof(DbContextOptions),
CreateDbContextOptions<TContextImplementation>,
Expand Down
15 changes: 10 additions & 5 deletions test/EFCore.Tests/DbContextTest.Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4044,8 +4044,8 @@ public void Throws_when_wrong_DbContextOptions_used()
public void Throws_when_adding_two_contexts_using_non_generic_options()
{
var appServiceProvider = new ServiceCollection()
.AddDbContext<NonGenericOptions1>(b => b.UseInMemoryDatabase(Guid.NewGuid().ToString()))
.AddDbContext<NonGenericOptions2>(b => b.UseInMemoryDatabase(Guid.NewGuid().ToString()))
.AddDbContext<NonGenericOptions1>(b => b.UseInMemoryDatabase(Guid.NewGuid().ToString()))
.BuildServiceProvider(validateScopes: true);

using var serviceScope = appServiceProvider
Expand Down Expand Up @@ -4078,26 +4078,31 @@ public NonGenericOptions2(DbContextOptions options)
}

[ConditionalFact]
public void AddDbContext_adds_single_options_for_all_types()
public void AddDbContext_adds_options_for_all_types()
{
var services = new ServiceCollection()
.AddSingleton<DbContextOptions>(_ => new DbContextOptions<NonGenericOptions1>())
.AddDbContext<NonGenericOptions1>(optionsLifetime: ServiceLifetime.Singleton)
.AddDbContext<NonGenericOptions2>(optionsLifetime: ServiceLifetime.Singleton)
.BuildServiceProvider(validateScopes: true);

Assert.Single(services.GetServices<DbContextOptions>());
Assert.Equal(3, services.GetServices<DbContextOptions>().Count());
Assert.Equal(
2, services.GetServices<DbContextOptions>()
.Select(o => o.ContextType)
.Distinct()
.Count());
}

[ConditionalFact]
public void First_DbContextOptions_in_serviceCollection_selected()
public void Last_DbContextOptions_in_serviceCollection_selected()
{
var services = new ServiceCollection()
.AddDbContext<NonGenericOptions1>(optionsLifetime: ServiceLifetime.Singleton)
.AddDbContext<NonGenericOptions2>(optionsLifetime: ServiceLifetime.Singleton)
.BuildServiceProvider(validateScopes: true);

Assert.Equal(typeof(NonGenericOptions1), services.GetService<DbContextOptions>().ContextType);
Assert.Equal(typeof(NonGenericOptions2), services.GetService<DbContextOptions>().ContextType);
}

[ConditionalFact]
Expand Down
Loading