[英]Using LINQ to query three entitites. - Include path expression must refer to a navigation property defined on the type
I am trying to get data from 3 entities. 我正在尝试从3个实体获取数据。 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=The Include path expression must refer to a navigation property defined on the type.
exceptionMessage =包含路径表达式必须引用在类型上定义的导航属性。 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))
Try this: 尝试这个:
var result = await db.Exams
.Include("Objectives.ObjectiveDetails")
.Where(e => e.Name == name)
.FirstOrDefaultAsync();
From http://msdn.microsoft.com/en-us/library/bb738708(v=vs.110).aspx 来自http://msdn.microsoft.com/zh-cn/library/bb738708(v=vs.110).aspx
Query paths can be used with Entity SQL and LINQ queries.
查询路径可以与Entity SQL和LINQ查询一起使用。 Paths are all-inclusive.
路径是包罗万象的。
For example, if an include call indicates Include("Orders.OrderLines"), not only will OrderLines be included, but also Orders.
例如,如果一个include调用指示Include(“ Orders.OrderLines”),则不仅将包括OrderLines,还将包括Orders。 For more information, see Shaping Query Results (Entity Framework).
有关更多信息,请参见对查询结果进行整形(实体框架)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.