Joshy
Joshy

Reputation: 657

Entity Framework Throws Error With Functions on a View

I have an repository that has has a method where I can pass functions to further query the data, for example;

List<Household> households = repository.AllIncluding(i => i.Id == "1419683").ToList();

When I connect to a table this call works but when I connect to a View it throws the following error: 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.

I thought it maybe be related to the relationships I have on the table but even when I query the Id (I know I can use find) it still throws the error. Please help!?

Here is my method in full:

    public virtual IQueryable<Household> AllIncluding(params Expression<Func<Household, object>>[] includeProperties)
    {
        IQueryable<Household> query = _dbContext.Households;
        foreach (var includeProperty in includeProperties)
        {
            query = query.Include(includeProperty);
        }
        return query;
    }

Upvotes: 2

Views: 157

Answers (1)

Mitul
Mitul

Reputation: 9854

Instead of this

List<Household> households = repository.AllIncluding(i => i.Id == "1419683").ToList();

just try this

var households = repository.AllIncluding().Where(i => i.Id == "1419683").ToList();

Another thing is that .Include accepts navigation properties which means linked tables. For example if you have scooters as linked entities then you could do something like this

var households = repository.AllIncluding(x => x.Scooters).Where(i => i.Id == "1419683").ToList();

Upvotes: 1

Related Questions