使用LINQ查询三个实体. -包含路径表达式必须引用在类型上定义的导航属性 [英] Using LINQ to query three entitites. - Include path expression must refer to a navigation property defined on the type

查看:88
本文介绍了使用LINQ查询三个实体. -包含路径表达式必须引用在类型上定义的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从3个实体中获取数据.考试,目的和目的细节.我希望能够通过考试名称来选择它.这是我正在使用的代码:

I am trying to get data from 3 entities. Exam, Objective and Objective detail. I want to be able to select this by exam name. Here is the code I am using:

        var result = await db.Exams
            .Include(e => e.Objectives)
            .Include(e => e.Objectives.SelectMany(o => o.ObjectiveDetails))
            .Where(e => e.Name == name)
            .FirstOrDefaultAsync();

当我运行它时,这给我一个错误消息:

This is giving me an error message when I run it saying:

exceptionMessage =包含路径表达式必须引用一个 在类型上定义的导航属性.使用虚线路径 参考导航属性和用于收集的Select运算符 导航属性.参数名称:路径

exceptionMessage=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. Parameter name: path

有人可以告诉我我在做什么错.这是我的课程:

Can someone advise me what I am doing wrong. Here's my classes:

public class Exam
{
    public Exam()
    {
        this.Objectives = new HashSet<Objective>();
    }
    public int ExamId { get; set; }
    public int SubjectId { get; set; }
    public virtual ICollection<Objective> Objectives { get; set; }
}

public class Objective : AuditableTable
{
    public Objective()
    {
        this.ObjectiveDetails = new HashSet<ObjectiveDetail>();
    }
    public int ObjectiveId { get; set; }
    public int ExamId { get; set; }
    public int Number { get; set; }
    public virtual Exam Exam { get; set; }
    public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }

}

public partial class ObjectiveDetail
{
    public int ObjectiveDetailId { get; set; }
    public int ObjectiveId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
    public virtual Objective Objective { get; set; }
}

推荐答案

使用Select代替SelectMany:

  .Include(e => e.Objectives.Select(o => o.ObjectiveDetails))

这篇关于使用LINQ查询三个实体. -包含路径表达式必须引用在类型上定义的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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