[英]entity framework core, generic repository method with inclusion navigation properties
我想通過業務邏輯項目創建通用數據訪問層。業務邏輯層必須與我擁有存儲庫的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)
這實際上是不好的設計。 您不想這樣做。 如果要抽象數據訪問層,請完全這樣做。 創建一個為您的應用程序提供API的庫,此庫將准確無誤地返回您所需的內容。 如果您仍然允許任意查詢,則直接使用您的上下文,因為您實際上除了購買要維護的其他層之外沒有購買任何東西,沒有任何實際好處。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.