我想爲EF使用自定義框架,但我有一個我不明白的問題。錯誤在下面。這似乎很明顯,但我不知道如何解決它實體框架,包含路徑表達式必須引用在類型上定義的導航屬性
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
參數名稱:路徑
我調用這個函數,它拋出上述異常。
_menuRepository.GetAll(d => d.DateToDisplay <= DateTime.Today).OrderByDescending(d=>d.Id).Take(100).ToList();
和GetAll()函數如下所示。
public virtual IList<T> GetAll(params Expression<Func<T, object>>[] navigationProperties)
{
List<T> list;
using (var context = new DBEntities())
{
IQueryable<T> dbQuery = context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty); //**** It throws the exception here
list = dbQuery
.AsNoTracking()
.ToList<T>();
}
return list;
}
我做錯了什麼?我怎樣才能解決這個問題?
我無法直接回答您的問題,但請注意,以這種方式使用EF會導致'OrderByDescending()'和'Take()'子句對內存中的記錄執行這些操作,而不是SQL正在從'GetAll()'方法返回'List'。意思是,如果您有10,000條記錄,您當前的方法將向您的應用程序發送10,000條記錄,然後在該應用程序中進行排序並取得其中的100條記錄。爲了防止,您希望將'.OrderBy'和'.Take()'語句應用於EF上下文查詢**在**之前調用'ToList()'。 –
Tommy
@Tommy,我明白你的意思了,我會照顧的。我怎麼才能像使用SQL一樣使用命令,然後將所有內容都放入內存中 –
只要可以肯定,你想要的是'Include'?而不是'Where'?因爲當你調用'GetAll'時,你似乎想要應用一個過濾器。 –