Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC4 tutorial - Eager loading of a property of a foreign key object, that has itself been eager loaded
    text
    copied!<p>I am working with MVC4 and EF5, and I have been working through the Contoso University tutorial on this page: <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application</a></p> <p>Relevant parts of the The Entity Framework model classes:</p> <pre><code>public class Student { public int StudentID { get; set; } public string LastName { get; set; } public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; } } public enum Grade { A,B,C,D,F} public class Enrollment { public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public Grade? Grade { get; set; } public Course Course { get; set; } public Student Student { get; set; } } public class Course { public int CourseID { get; set; } public string Title { get; set; } public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; } } </code></pre> <p>In the Controller:</p> <pre><code>public ActionResult Details(int id = 0) { Student student = db.Students.Find(id); return View(student); } </code></pre> <p>The relevant bit of the view:</p> <pre><code>@foreach (var item in Model.Enrollments) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Course.Title) //this does not display as Course is null &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Grade) &lt;/td&gt; &lt;/tr&gt; } </code></pre> <p>Contrary to what the tutorial says, for me the view does not display the Course Title, it only shows the Grade.</p> <pre><code>Course Title Grade A C B </code></pre> <p>Debugging shows that the Course property (representing the foreign key to Course) is null. In other words, for each Student, the Enrollments collection is loaded, but those enrollments have not loaded their Course property. I cannot work out how to use Eager Loading to load the Course property of each Enrollment. How do I get the Course Title to display in the view?</p> <p>I have found that a hard coded "Include" string works (in the Controller):</p> <pre><code>Student student = db.Students.Where(x =&gt; x.StudentID == id).Include("Enrollments.Course").Single(); </code></pre> <p>But I cannot work out how to do it with a lambda function or any other function.</p> <p>I have tried the following with no success:</p> <pre><code>Student student = db.Students.Where(x =&gt; x.StudentID == id).Include(y =&gt; y.Enrollments.Course).Single(); Student student = db.Students.Where(x =&gt; x.StudentID == id).Include(y =&gt; y.Enrollments).Include(z =&gt; z.Enrollments.Course).Single(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload