Skip to content

Commit 93698d0

Browse files
committed
Fixed last broken test
1 parent 844bd85 commit 93698d0

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/EntityFrameworkCore.Triggered/Extensions/ServiceCollectionExtensions.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public static IServiceCollection AddTriggeredPooledDbContextFactory<TContext>(th
127127

128128
var serviceDescriptor = serviceCollection.FirstOrDefault(x => x.ServiceType == typeof(IDbContextFactory<TContext>));
129129

130+
#if EFCORETRIGGERED2
130131
if (serviceDescriptor?.ImplementationType != null)
131132
{
132133
var triggeredFactoryType = typeof(TriggeredDbContextFactory<,>).MakeGenericType(typeof(TContext), serviceDescriptor.ImplementationType);
@@ -143,13 +144,20 @@ public static IServiceCollection AddTriggeredPooledDbContextFactory<TContext>(th
143144
lifetime: ServiceLifetime.Scoped
144145
));
145146
}
146-
else if (serviceDescriptor?.ImplementationFactory != null)
147+
#elif EFCORETRIGGERED3
148+
if (serviceDescriptor?.ImplementationFactory != null)
147149
{
148-
throw new NotImplementedException();
149-
}
150+
var triggeredFactoryType = typeof(TriggeredDbContextFactory<>).MakeGenericType(typeof(TContext));
150151

152+
serviceCollection.Replace(ServiceDescriptor.Describe(
153+
serviceType: typeof(IDbContextFactory<TContext>),
154+
implementationFactory: serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider, triggeredFactoryType, serviceDescriptor.ImplementationFactory),
155+
lifetime: ServiceLifetime.Scoped
156+
));
157+
}
158+
#endif
151159
return serviceCollection;
152160
}
153-
#endif
154161
}
155162
}
163+
#endif

src/EntityFrameworkCore.Triggered/Internal/TriggeredDbContextFactory.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,34 @@ public TContext CreateDbContext()
3333
return context;
3434
}
3535
}
36+
37+
public sealed class TriggeredDbContextFactory<TContext> : IDbContextFactory<TContext>
38+
where TContext : DbContext
39+
{
40+
readonly Func<IServiceProvider, IDbContextFactory<TContext>> _contextFactoryFactory;
41+
readonly IServiceProvider _serviceProvider;
42+
43+
public TriggeredDbContextFactory(Func<IServiceProvider, IDbContextFactory<TContext>> contextFactoryFactory, IServiceProvider serviceProvider)
44+
{
45+
_contextFactoryFactory = contextFactoryFactory;
46+
_serviceProvider = serviceProvider;
47+
}
48+
49+
public TContext CreateDbContext()
50+
{
51+
var contextFactory = _contextFactoryFactory(_serviceProvider);
52+
var context = contextFactory.CreateDbContext();
53+
Debug.Assert(context is not null);
54+
55+
var applicationTriggerServiceProviderAccessor = context.GetService<ApplicationTriggerServiceProviderAccessor>();
56+
if (applicationTriggerServiceProviderAccessor != null)
57+
{
58+
applicationTriggerServiceProviderAccessor.SetTriggerServiceProvider(new HybridServiceProvider(_serviceProvider, context));
59+
}
60+
61+
return context;
62+
}
63+
}
64+
3665
#endif
3766
}

0 commit comments

Comments
 (0)