Skip to content

Commit 6c1c0ca

Browse files
committed
Added AddTriggeredDbContextPool that accepts an additional contract type
1 parent a402e84 commit 6c1c0ca

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/EntityFrameworkCore.Triggered/Extensions/ServiceCollectionExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static IServiceCollection AddTriggeredDbContext<TContext>(this IServiceCo
3434
return serviceCollection;
3535
}
3636

37-
public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 128)
37+
public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 1024)
3838
where TContext : DbContext
3939
{
4040
serviceCollection.AddDbContextPool<TContext>(options => {
@@ -55,6 +55,27 @@ public static IServiceCollection AddTriggeredDbContextPool<TContext>(this IServi
5555
return serviceCollection;
5656
}
5757

58+
public static IServiceCollection AddTriggeredDbContextPool<TContext, TImplementation>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, int poolSize = 1024)
59+
where TContext : class where TImplementation : DbContext, TContext
60+
{
61+
serviceCollection.AddDbContextPool<TContext, TImplementation>(options => {
62+
optionsAction?.Invoke(options);
63+
options.UseTriggers();
64+
}, poolSize);
65+
66+
var serviceDescriptor = serviceCollection.FirstOrDefault(x => x.ServiceType == typeof(TContext));
67+
if (serviceDescriptor?.ImplementationFactory != null)
68+
{
69+
serviceCollection.Replace(ServiceDescriptor.Describe(
70+
serviceType: typeof(TContext),
71+
implementationFactory: serviceProvider => SetApplicationTriggerServiceProviderAccessor(serviceDescriptor.ImplementationFactory(serviceProvider), serviceProvider),
72+
lifetime: ServiceLifetime.Transient
73+
));
74+
}
75+
76+
return serviceCollection;
77+
}
78+
5879
public static IServiceCollection AddTriggeredDbContextFactory<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime lifetime = ServiceLifetime.Singleton)
5980
where TContext : DbContext
6081
{

test/EntityFrameworkCore.Triggered.Tests/Infrastructure/ServiceCollectionExtensionsTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,27 @@ public void AddTriggeredDbContextPool_SupportsAScopedLifetime()
143143
Assert.Equal(context1, context1);
144144
}
145145

146+
[Fact]
147+
public void AddTriggeredDbContextPool_SupportAContractType()
148+
{
149+
var subject = new ServiceCollection();
150+
subject.AddTriggeredDbContextPool<DbContext, TestDbContext>(options => {
151+
options.UseInMemoryDatabase("test");
152+
options.ConfigureWarnings(warningOptions => {
153+
warningOptions.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
154+
});
155+
});
156+
157+
var serviceProvider = subject.BuildServiceProvider();
158+
159+
using var scope = serviceProvider.CreateScope();
160+
161+
var context1 = scope.ServiceProvider.GetRequiredService<DbContext>();
162+
var context2 = scope.ServiceProvider.GetRequiredService<TestDbContext>();
163+
164+
Assert.Equal(context1, context1);
165+
}
166+
146167
#if EFCORETRIGGERED2 || EFCORETRIGGERED3
147168
[Fact]
148169
public void AddTriggeredDbContextFactory_ReusesScopedServiceProvider()

0 commit comments

Comments
 (0)