使用getwithinclude方法连接两个存储库模式

时间:2017-07-26 10:24:50

标签: c# entity-framework-6 automapper unit-of-work

我正在学习UnitofWork,Repository模式,在genericrepository文件中,我有一个这样的方法,我不知道如何在实例中使用它。

/// <summary>
/// Include multiple
/// </summary>
/// <param name="predicate"></param>
/// <param name="include"></param>
/// <returns></returns>
public IQueryable<TEntity> GetWithInclude(System.Linq.Expressions.Expression<Func<TEntity,
bool>> predicate, params string[] include)
{
    IQueryable<TEntity> query = this.DbSet;
    query = include.Aggregate(query, (current, inc) => current.Include(inc));
    return query.Where(predicate);
}

例如,如果我们有两个存储库rep1,具有外键关系的rep2将连接两个存储库并将其输出到列表,我知道如何从单个存储库获取详细信息。

public IEnumerable<CREntity> GetAllCR()
{
    var mapper = CreateMapper();
    var CRAll = _unitOfWork.Rep1.GetAll().ToList();
    //var joinFD = _unitOfWork.Rep1.GetWithInclude(); ---how to add Rep2 using getwithinclude
    if (CRAll.Any())
    {
        var CRModel = mapper.Map<List<ChangeRequest>, List<CREntity>>(CRAll);
        return CRModel;
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

这是我的代码中的一个例子:

public List<TblUser> GetUserListWithRoles()
{
    GenericRepository<TblUser> repository = new GenericRepository<TblUser>(_context);

    List<TblUser> userList = new List<TblUser>();
    string[] includes = { "TblUserRol", "TblUserRol.FkIdRolNavigation" };
    userList = repository.GetWithInclude(c => c.State == true, includes).ToList();

    return userList;
}