实体框架核心3.1抛出“包含已用于非实体可查询”。例外 [英] Entity Framework Core 3.1 is throwing "Include has been used on non entity queryable" exception

查看:142
本文介绍了实体框架核心3.1抛出“包含已用于非实体可查询”。例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于ASP.NET Core 3.1的项目,其中我使用Entity Framework Core 3.1作为ORM。

I have an ASP.NET Core 3.1 based project where I am using Entity Framework Core 3.1 as an 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 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();

但是该代码抛出此错误


包含已用于非实体可查询

Include has been used on non entity queryable

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

What could be causing this problem? How can I fix it?

推荐答案

在引用父实体后立即放置您的包含文件。
您可以执行ThenInclude来同时包含所包含实体的子实体。

Put your includes right after referencing the parent entity. You can do ThenInclude to bring the child entities of the included entities also. You'll need to do one Include for each ThenInclude.

然后,您可以在包含/过滤后进行选择。像这样的东西:

Then you can do your selecting after including / filtering. Something like:

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

这篇关于实体框架核心3.1抛出“包含已用于非实体可查询”。例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆