我有一个基于ASP.NET Core 3.1的项目,其中使用Entity Framework Core 3.1作为ORM。
我有以下两个实体模型
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; }
}
我正在尝试查询用户相关联的所有属性。 PropertyToList
对象告诉我一个用户是否与某个属性相关联。 这是我所做的:
// 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();
但是这段代码会抛出以下错误
在非实体查询可执行项上使用了Include
可能是什么原因导致了这个问题?我该如何解决?