【问题标题】:Entity Framework Core 3.1 is throwing "Include has been used on non entity queryable" exceptionEntity Framework Core 3.1 抛出“Include has been used on non entity queryable”异常
【发布时间】:2020-06-07 10:40:08
【问题描述】:

我有一个基于 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();

但是那个代码抛出了这个错误

包含已用于非实体可查询

什么可能导致这个问题?我该如何解决?

【问题讨论】:

    标签: entity-framework asp.net-core .net-core entity-framework-core .net-core-3.1


    【解决方案1】:

    在引用父实体之后立即放置您的包含。 您也可以执行 ThenInclude 以带来包含实体的子实体。您需要为每个 ThenInclude 执行一个 Include。

    然后您可以在包含/过滤后进行选择。比如:

    var query = DataContext.PropertyToLists
    .Include(p => p.Property).ThenInclude(p => p.City)
    .Include(p => p.Property).ThenInclude(p => p.Type)
    .Include(p => p.Property).ThenInclude(p => p.Status)
    .Where(p => p.Selected == true && p.UserId == userId && p.ListId == listId)
    .Select(p => p.Property);
    

    【讨论】:

    • 谢谢。这实际上在 EF 6 上是相反的,为了避免一些错误,你必须把它们放在最后。
    【解决方案2】:

    观察,您的域模型 PropertyToList 和 Property 都具有虚拟属性。最重要的是,您正在使用 Include 运算符来选择这些属性。

    这不是必需的,当属性定义为 virtual 时,它将被延迟加载。所以不需要包含。不推荐延迟加载方式,使用 include 更好,因此您只选择所需的图形属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      相关资源
      最近更新 更多