Reputation:
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=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; }
}
Upvotes: 0
Views: 8778
Reputation: 152626
Use Select
instead of SelectMany:
.Include(e => e.Objectives.Select(o => o.ObjectiveDetails))
Upvotes: 3
Reputation: 3082
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
Query paths can be used with Entity SQL and LINQ queries. Paths are all-inclusive.
For example, if an include call indicates Include("Orders.OrderLines"), not only will OrderLines be included, but also Orders. For more information, see Shaping Query Results (Entity Framework).
Upvotes: 1