【发布时间】:2017-11-04 12:49:08
【问题描述】:
我想通过业务逻辑项目创建一个通用数据访问层,业务逻辑层必须与我拥有存储库的 DAL 层交互。 我想要一个短暂的数据上下文和所有返回 IEnumerable 而不是 IQueryable 的存储库方法。 提示 GetAll 方法示例如何通过 ThenInclude 或 Select 将连接的数据提取到依赖表。
public class GenericDataRepository<T> : IGenericDataRepository<T> where T : class
{
public virtual IList<T> GetAll(params Expression<Func<T, object>>[] navigationProperties)
{
List<T> list;
using (var context = new Context())
{
IQueryable<T> dbQuery = context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
list = dbQuery
.AsNoTracking()
.ToList<T>();
}
return list;
}
}
然后包含(prop2=>prop2.i)?
dbQuery.Include(navigationProperty).ThenInclude(prop2=>prop2.i)
【问题讨论】:
标签: c# entity-framework c#-4.0 asp.net-core