首页 > 解决方案 > 如何使用 Autofac 注册 DbContextPool?

问题描述

微软的 DI 有AddDbContextPool,但是怎么做呢Autofac

标签: c#entity-framework-coreautofac

解决方案


因此,我猜想,“如何注册 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);
}

文档


推荐阅读