ayilmaz ayilmaz - 1 year ago 531
C# Question

Entity Framework, The Include path expression must refer to a navigation property defined on the type

I am trying to use custom framework for EF but I am having a problem that I dont understand. The error is below. It seems very obvious but I am not sure how to fix it

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.

Parameter name: path

I am calling this function and it throws the exception above.

_menuRepository.GetAll(d => d.DateToDisplay <= DateTime.Today).OrderByDescending(d=>d.Id).Take(100).ToList();

and GetAll() function is like below.

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
return list;

What am I doing wrong? How can I fix this?

Answer Source

An Expression in EF is expected to be a lambda that retrieves a property of an object. In your example

d => d.DateToDisplay <= DateTime.Today

is a predicate - it evaluates to a Boolean value: that's why Include cannot determine which property to "include".

I guess you've confused Include with a LINQ Where clase. Apparently, this should work:

_menuRepository.GetAll(d => d.DateToDisplay).OrderByDescending(d=>d.Id).Take(100).ToList();

if only type T has property DateToDisplay.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download