Skip to content

Commit 5d88b84

Browse files
authored
Allow the last DbContext to be registered to use nongeneric options (#32569)
This is important for Identity
1 parent db9846d commit 5d88b84

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/EFCore/Extensions/EntityFrameworkServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ private static void AddCoreServices<TContextImplementation>(
11341134
CreateDbContextOptions<TContextImplementation>,
11351135
optionsLifetime));
11361136

1137-
serviceCollection.TryAdd(
1137+
serviceCollection.Add(
11381138
new ServiceDescriptor(
11391139
typeof(DbContextOptions),
11401140
CreateDbContextOptions<TContextImplementation>,

test/EFCore.Tests/DbContextTest.Services.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4044,8 +4044,8 @@ public void Throws_when_wrong_DbContextOptions_used()
40444044
public void Throws_when_adding_two_contexts_using_non_generic_options()
40454045
{
40464046
var appServiceProvider = new ServiceCollection()
4047-
.AddDbContext<NonGenericOptions1>(b => b.UseInMemoryDatabase(Guid.NewGuid().ToString()))
40484047
.AddDbContext<NonGenericOptions2>(b => b.UseInMemoryDatabase(Guid.NewGuid().ToString()))
4048+
.AddDbContext<NonGenericOptions1>(b => b.UseInMemoryDatabase(Guid.NewGuid().ToString()))
40494049
.BuildServiceProvider(validateScopes: true);
40504050

40514051
using var serviceScope = appServiceProvider
@@ -4078,26 +4078,31 @@ public NonGenericOptions2(DbContextOptions options)
40784078
}
40794079

40804080
[ConditionalFact]
4081-
public void AddDbContext_adds_single_options_for_all_types()
4081+
public void AddDbContext_adds_options_for_all_types()
40824082
{
40834083
var services = new ServiceCollection()
40844084
.AddSingleton<DbContextOptions>(_ => new DbContextOptions<NonGenericOptions1>())
40854085
.AddDbContext<NonGenericOptions1>(optionsLifetime: ServiceLifetime.Singleton)
40864086
.AddDbContext<NonGenericOptions2>(optionsLifetime: ServiceLifetime.Singleton)
40874087
.BuildServiceProvider(validateScopes: true);
40884088

4089-
Assert.Single(services.GetServices<DbContextOptions>());
4089+
Assert.Equal(3, services.GetServices<DbContextOptions>().Count());
4090+
Assert.Equal(
4091+
2, services.GetServices<DbContextOptions>()
4092+
.Select(o => o.ContextType)
4093+
.Distinct()
4094+
.Count());
40904095
}
40914096

40924097
[ConditionalFact]
4093-
public void First_DbContextOptions_in_serviceCollection_selected()
4098+
public void Last_DbContextOptions_in_serviceCollection_selected()
40944099
{
40954100
var services = new ServiceCollection()
40964101
.AddDbContext<NonGenericOptions1>(optionsLifetime: ServiceLifetime.Singleton)
40974102
.AddDbContext<NonGenericOptions2>(optionsLifetime: ServiceLifetime.Singleton)
40984103
.BuildServiceProvider(validateScopes: true);
40994104

4100-
Assert.Equal(typeof(NonGenericOptions1), services.GetService<DbContextOptions>().ContextType);
4105+
Assert.Equal(typeof(NonGenericOptions2), services.GetService<DbContextOptions>().ContextType);
41014106
}
41024107

41034108
[ConditionalFact]

0 commit comments

Comments
 (0)