C# 使用getwithinclude方法连接两个存储库模式
我正在学习UnitofWork,Repository模式,在genericrepository文件中我有一个这样的方法,我不知道如何在实际示例中使用它C# 使用getwithinclude方法连接两个存储库模式,c#,entity-framework-6,automapper,unit-of-work,C#,Entity Framework 6,Automapper,Unit Of Work,我正在学习UnitofWork,Repository模式,在genericrepository文件中我有一个这样的方法,我不知道如何在实际示例中使用它 /// <summary> /// Include multiple /// </summary> /// <param name="predicate"></param> /// <param name="include"></param> /// <returns&
/// <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);
}
//
///包括多个
///
///
///
///
公共IQueryable GetWithInclude(System.Linq.Expressions.Expression谓词,参数字符串[]include)
{
IQueryable query=this.DbSet;
query=include.Aggregate(query,(current,inc)=>current.include(inc));
返回query.Where(谓词);
}
例如,如果我们有两个具有foreignkey关系的存储库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;
}
public IEnumerable GetAllCR()
{
var mapper=CreateMapper();
var CRAll=_unitOfWork.Rep1.GetAll().ToList();
//var joinFD=_unitOfWork.Rep1.GetWithInclude();---如何使用GetWithInclude添加Rep2
if(CRAll.Any())
{
var CRModel=mapper.Map(CRAll);
返回模型;
}
返回null;
}
这是我的代码中的一个示例:
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;
}
public List GetUserListWithRoles()
{
GenericRepository repository=新的GenericRepository(_上下文);
List userList=新列表();
字符串[]包括={“TblUserRol”,“TblUserRol.FkIdRolNavigation”};
userList=repository.GetWithInclude(c=>c.State==true,includes).ToList();
返回用户列表;
}
看起来您需要做的就是var data=Rep1.GetWithInclude(x=>x.ID==5,“ChildEntity1”,ChildEntity2)代码>感谢您的回复,一个澄清是什么是childentity1和childentity2。。。它们是entityclass名称Rep1和Rep2还是FK名称?Include
必须包含类CREntity
的导航属性名称,而不是存储库名称。