I'm trying to learn Entity Framework 6 code first using VS 2013.
I'm doing the exact same thing as in this question: How to eagerly load a many to many relationship with the entity framework code first?
Using the same setup
public class Student
{
public int Id {get;set}
public string FullName {get;set;}
public virtual ICollection<Course> Courses {get;set;}
}
public class Course
{
public int Id {get;set;}
public string FullName {get;set;}
public virtual ICollection<Student> Students {get;set;}
}
And the answer
var allStudents = context.Students.Include( s => s.Courses);
But when debugging I get a recursive result. Every Student contains a list of Courses and those Courses contains a list of Students, which contains Courses, that contains Students and so on....
The same thing without using the method .Include(...
var allStudents = context.Students;
I'm using this in a scaffolded MVC 5 ApiController which throws:
System.Runtime.Serialization.SerializationException
Removing Virtual
from the Models and still using .Include(...
throws:
Object graph for type 'SchoolEF.Models.Course' contains cycles and cannot be serialized if reference tracking is disabled.
I guess i have not fully understood how to do eager loading and how to use the .Include() method.