C# 具有通用存储库的DbContextScope

C# 具有通用存储库的DbContextScope,c#,entity-framework,generics,repository-pattern,dbcontext,C#,Entity Framework,Generics,Repository Pattern,Dbcontext,我正在使用所描述的DbContextScope 在如何在实例化的类之外获取dbcontext的示例中,Mehdi写道: public class UserRepository : IUserRepository { private readonly IAmbientDbContextLocator _contextLocator; public UserRepository(IAmbientDbContextLocator contextLocator) {

我正在使用所描述的DbContextScope

在如何在实例化的类之外获取dbcontext的示例中,Mehdi写道:

public class UserRepository : IUserRepository {
    private readonly IAmbientDbContextLocator _contextLocator;

    public UserRepository(IAmbientDbContextLocator contextLocator)
    {
        if (contextLocator == null) throw new ArgumentNullException("contextLocator");
        _contextLocator = contextLocator;
    }

    public User Get(Guid id)
    {
        return _contextLocator.Get<MyDbContext>.Set<User>().Find(id);
    } 
}
公共类用户存储库:IUserRepository{
私有只读IAmbientDbContextLocator\u contextLocator;
公共用户存储库(IAmbientDbContextLocator contextLocator)
{
如果(contextLocator==null)抛出新的ArgumentNullException(“contextLocator”);
_contextLocator=contextLocator;
}
公共用户获取(Guid id)
{
返回_contextLocator.Get.Set().Find(id);
} 
}
但是,如果我想要一个通用存储库,比如

public abstract class RepositoryBase<T> : IRepository<T> where T : class, IDomainEntity
{
    private readonly DbSet<T> set;

    private IAmbientDbContextLocator contextLocator;

    protected RepositoryBase(IAmbientDbContextLocator ctxLocator)
    {
        if (ctxLocator == null) throw new ArgumentNullException(nameof(ctxLocator));
        contextLocator = ctxLocator;
    }

    public T Get(Guid id)
    {
        //return _contextLocator.Get<MyDbContext>.Set<T>().Find(userId);
    }
}
公共抽象类RepositoryBase:IRepository,其中T:class,IDomainEntity
{
专用只读数据库集;
私有IAmbientDbContextLocator上下文定位器;
受保护的RepositoryBase(IAmbientDbContextLocator ctxLocator)
{
如果(ctxLocator==null)抛出新的ArgumentNullException(nameof(ctxLocator));
contextLocator=ctxLocator;
}
公共无法获取(Guid id)
{
//返回_contextLocator.Get.Set().Find(userId);
}
}
那么dbset应该如何解决呢?如何在Get方法中使用“MyDbContext”? 我确实有多个上下文。

公共抽象类RepositoryBase:IRepository其中T:IDomainEntity其中TDbContext:DbContext
   public abstract class RepositoryBase<T, TDbContext> : IRepository<T> where T : IDomainEntity where TDbContext : DbContext
    {
        private readonly DbSet<T> _dbset;
        private readonly IAmbientDbContextLocator _contextLocator;

        protected RepositoryBase(IAmbientDbContextLocator ctxLocator)
        {
            if (ctxLocator == null) throw new ArgumentNullException(nameof(ctxLocator));
            _contextLocator = ctxLocator;
            _dbset = _contextLocator.Get<TDbContext>.Set<T>();
        }

        protected DbSet<T> DbSet { get { return _dbset; } }
        public T Get(Guid id)
        {
            return DbSet.Find(id);
        }
    }
{ 私有只读数据库集_DbSet; 私有只读IAmbientDbContextLocator\u contextLocator; 受保护的RepositoryBase(IAmbientDbContextLocator ctxLocator) { 如果(ctxLocator==null)抛出新的ArgumentNullException(nameof(ctxLocator)); _contextLocator=ctxLocator; _dbset=_contextLocator.Get.Set(); } 受保护的DbSet DbSet{get{return\u DbSet;}} 公共无法获取(Guid id) { 返回DbSet.Find(id); } }
如果不需要
TDbContext
,可以在
contextlocator
旁边的构造函数上发送
DbContext
。但他强迫您使用
DbContextScope
,我并没有阅读所有文章,但我们不要破坏他的逻辑