简体   繁体   中英

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;
    }

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM