how to use multi DBContextPool?

前端 未结 2 1401
自闭症患者
自闭症患者 2021-01-21 06:05

In EFCore 2.0 Add new feature, DbContext pooling. i know how to use it in single context, however, sometimes need multi context in project,

public class BContext         


        
相关标签:
2条回答
  • 2021-01-21 06:28

    Or you can register your own factory of DbContextPool

    services.AddDbContextPool<AContext>(options =>
    {
        options.UseInMemoryDatabase("AContext.InMemory");
    });
    services.AddDbContextPool<BContext>(options =>
    {
        options.UseInMemoryDatabase("BContext.InMemory");
    });
    
    collection.AddSingleton(svcs => new DbContextPool<AContext>(svcs.GetService<DbContextOptions<AContext>>()));
    collection.AddSingleton(svcs => new DbContextPool<BContext>(svcs.GetService<DbContextOptions<BContext>>()));
    
    0 讨论(0)
  • 2021-01-21 06:30

    Ok. i've found problem. You need to download EF Core, then change constructor for DbContextPool< TContext>

    original

    public DbContextPool([NotNull] DbContextOptions options)
    

    and change to

    public DbContextPool([NotNull] DbContextOptions<TContext> options)
    

    otherwise DI will use last added options :)

    0 讨论(0)
提交回复
热议问题