我正在尝试实现描述的存储库模式 here
但是,这些关系并未包含在内。
public IEnumerable<MyEntity> GetWithRelationship(params Expression<Func<MyEntity, object>>[] includeProperties)
{
var set = _context.MyEntities;
foreach (var includeProperty in includeProperties)
{
set.Include(includeProperty);
}
return set.ToList();
}
但是以下方法有效:
return _context.MyEntities.Include(x => x.RelatedEntity).ToList();
最佳答案
您必须重新分配set
到 Include()
的返回值方法。但是,为了避免编译错误,您必须处理 set
如IQueryable<MyEntity>
第一:
var set = _context.MyEntities as IQueryable<MyEntity>;
foreach (var includeProperty in includeProperties)
{
set = set.Include(includeProperty);
}
return set.ToList();
关于asp.net-core - 将 Lambda 作为参数传递到 EFCore 中的存储库方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250040/