I have an ASP.NET Core 3.1 based project where I am using Entity Framework Core 3.1 as an ORM.
I have the following two entity models
public class PropertyToList
{
public int Id { get; set; }
public int ListId { get; set; }
public int UserId { get; set; }
public int PropertyId { get; set; }
// ... Other properties removed for the sake of simplicity
public virtual Property Property { get; set; }
}
public class Property
{
public int Id { get; set; }
// ... Other properties removed for the sake of simplicity
public int TypeId { get; set; }
public int StatusId { get; set; }
public int CityId { get; set; }
public virtual Type Type { get; set; }
public virtual Status Status { get; set; }
public virtual City City { get; set; }
}
I am trying to query all properties where a user has relation to. The PropertyToList
object tells me if a user is related to a property. Here is what I have done
// I start the query at a relation object
IQueryable<Property> query = DataContext.PropertyToLists.Where(x => x.Selected == true)
.Where(x => x.UserId == userId && x.ListId == listId)
// After identifying the relations that I need,
// I only need to property object "which is a virtual property in" the relation object
.Select(x => x.Property)
// Here I am including relations from the Property virtual property which are virtual properties
// on the Property
.Include(x => x.City)
.Include(x => x.Type)
.Include(x => x.Status);
List<Property> properties = await query.ToListAsync();
But that code is throwing this error
Include has been used on non entity queryable
What could be causing this problem? How can I fix it?
An observation, your domain models PropertyToList and Property both have virtual properties. On top of that you are using Include operator to select these properties.
This is not necessary, when property is defined with virtual, then it will be lazy loaded. So an Include is not needed. Lazy loading is not the recommended way, using include is better so you select only graph properties that are required.