1- using EntityFrameworkCore . Triggered . Tests . Stubs ;
1+ using System ;
2+ using EntityFrameworkCore . Triggered . Tests . Stubs ;
23using Microsoft . EntityFrameworkCore ;
34using Microsoft . EntityFrameworkCore . Diagnostics ;
45using Microsoft . EntityFrameworkCore . Infrastructure ;
6+ using Microsoft . EntityFrameworkCore . Internal ;
57using Microsoft . Extensions . DependencyInjection ;
68using 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