使用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))