Skip to content

Commit e910766

Browse files
committed
Fixed an issue with correctly registering a custom DbContextFactory
1 parent 5d2649c commit e910766

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/EntityFrameworkCore.Triggered/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static IServiceCollection AddTriggeredDbContextFactory<TContext, TFactory
9292
where TContext : DbContext
9393
where TFactory : IDbContextFactory<TContext>
9494
{
95-
serviceCollection.AddDbContextFactory<TContext>(options => {
95+
serviceCollection.AddDbContextFactory<TContext, TFactory>(options => {
9696
optionsAction?.Invoke(options);
9797
options.UseTriggers();
9898
}, lifetime);

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using EntityFrameworkCore.Triggered.Tests.Stubs;
1+
using System;
2+
using EntityFrameworkCore.Triggered.Tests.Stubs;
23
using Microsoft.EntityFrameworkCore;
34
using Microsoft.EntityFrameworkCore.Diagnostics;
45
using Microsoft.EntityFrameworkCore.Infrastructure;
6+
using Microsoft.EntityFrameworkCore.Internal;
57
using Microsoft.Extensions.DependencyInjection;
68
using Xunit;
79

@@ -21,6 +23,15 @@ public TestDbContext(DbContextOptions options) : base(options)
2123
}
2224
}
2325

26+
#if EFCORETRIGGERED2
27+
class TestDbContextFactory : DbContextFactory<TestDbContext>
28+
{
29+
public TestDbContextFactory(IServiceProvider serviceProvider, DbContextOptions<TestDbContext> options, IDbContextFactorySource<TestDbContext> factorySource) : base(serviceProvider, options, factorySource)
30+
{
31+
}
32+
}
33+
#endif
34+
2435
[Fact]
2536
public void AddTriggeredDbContext_AddsTriggersAndCallsUsersAction()
2637
{
@@ -142,6 +153,34 @@ public void AddTriggeredDbContextFactory_ReusesScopedServiceProvider()
142153
Assert.Equal(1, triggerStub.BeforeSaveInvocations.Count);
143154
}
144155

156+
[Fact]
157+
public void AddTriggeredDbContextFactory_WithCustomFactory_ReusesScopedServiceProvider()
158+
{
159+
var subject = new ServiceCollection();
160+
subject.AddTriggeredDbContextFactory<TestDbContext, TestDbContextFactory>(options => {
161+
options.UseInMemoryDatabase("test");
162+
options.ConfigureWarnings(warningOptions => {
163+
warningOptions.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
164+
});
165+
}).AddScoped<IBeforeSaveTrigger<TestModel>, TriggerStub<TestModel>>();
166+
167+
var serviceProvider = subject.BuildServiceProvider();
168+
169+
using var scope = serviceProvider.CreateScope();
170+
171+
var contextFactory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<TestDbContext>>();
172+
173+
var context = contextFactory.CreateDbContext();
174+
175+
context.TestModels.Add(new TestModel());
176+
177+
context.SaveChanges();
178+
179+
var triggerStub = scope.ServiceProvider.GetRequiredService<IBeforeSaveTrigger<TestModel>>() as TriggerStub<TestModel>;
180+
Assert.NotNull(triggerStub);
181+
Assert.Equal(1, triggerStub.BeforeSaveInvocations.Count);
182+
}
183+
145184
[Fact]
146185
public void AddTriggeredPooledDbContextFactory_ReusesScopedServiceProvider()
147186
{

0 commit comments

Comments
 (0)