如何使用 Autofac 注册 DbContextPool?
How to register DbContextPool using Autofac?
Microsoft 的 DI 有 AddDbContextPool
但如何使用 Autofac
?
所以,我想,对于像“如何注册 Microsoft 的 DI(日志记录、DbContext 等)中存在的东西?”这样的情况,标准的解决方法是使用 ServiceCollection
然后调用 Populate
方法。
我对DbContextPool
的认识:
public static void RegisterDbContextPool<T>(this ContainerBuilder builder, Action<DbContextOptionsBuilder> options) where T : DbContext {
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContextPool<T>(options);
builder.Populate(serviceCollection);
}
Microsoft 的 DI 有 AddDbContextPool
但如何使用 Autofac
?
所以,我想,对于像“如何注册 Microsoft 的 DI(日志记录、DbContext 等)中存在的东西?”这样的情况,标准的解决方法是使用 ServiceCollection
然后调用 Populate
方法。
我对DbContextPool
的认识:
public static void RegisterDbContextPool<T>(this ContainerBuilder builder, Action<DbContextOptionsBuilder> options) where T : DbContext {
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContextPool<T>(options);
builder.Populate(serviceCollection);
}