title: Child collection is null in ASP.NET web API using Entity Framework
body: <p>I'm trying to create an ASP.NET web service that exposes a list of students and courses with the Entity Framework. Each Course is composed of multiple Students:</p>
  
  <pre><code>public class Student
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  public class Course
  {
      public int Id { get; set; }
      public string Title { get; set; }
      public ICollection&lt;Student&gt; Students { get; set; }
  }
  </code></pre>
  
  <p>I am seeding the models like so:</p>
  
  <pre><code>protected override void Seed(StudentsContext context)
  {
      var students = new List&lt;Student&gt; 
      {
          new Student { Id = 1, Name = "stu1" },
          new Student { Id = 2, Name = "stu2" },
          new Student { Id = 3, Name = "stu3" },
      };
  
      students.ForEach(s =&gt; context.Students.Add(s));
      context.SaveChanges();
  
      var courses = new List&lt;Course&gt; 
      {
          new Course { Id = 1, Title = "course1",
              Students = new List&lt;Student&gt;() { students[0], students[1] } },
          new Course { Id = 2, Title = "course2", 
              Students = new List&lt;Student&gt;() { students[0] } },         
          new Course { Id = 3, Title = "course3" },
      };
  
      courses.ForEach(c =&gt; context.Courses.Add(c));
      context.SaveChanges();
  }
  </code></pre>
  
  <p>Visual Studio generated an <code>ApiController</code> for the <code>Course</code> with a <code>GetCourses()</code> method to get all the courses:</p>
  
  <pre><code>// GET api/Course
  public IEnumerable&lt;Course&gt; GetCourses()
  {
      return db.Courses.AsEnumerable();
  }
  </code></pre>
  
  <p>However, when I use the web service to get the list of courses, it returns back JSON with courses that have students set to null:</p>
  
  <pre><code>[{"Id":1,"Title":"course1","Students":null},
   {"Id":2,"Title":"course2","Students":null},
   {"Id":3,"Title":"course3","Students":null}]
  </code></pre>
  
  <p>How could I get the web service to include the list of students that are assigned to each course?  Thanks in advance.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Slauma's fix of changing <code>GetCourses</code> to</p>
  
  <pre><code>public IEnumerable&lt;Course&gt; GetCourses()
  {
      return db.Courses.Include(c =&gt; c.Students).AsEnumerable();
  }
  </code></pre>
  
  <p><em>partially</em> worked.  martin_costello's second solution had a similar effect. Something else I needed to do was to fix my Student class to clearly indicate the many-to-many relationship:</p>
  
  <pre><code>public class Student
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public ICollection&lt;Course&gt; CoursesAttending { get; set; }
  }
  
  public class Course
  {
      public int Id { get; set; }
      public string Title { get; set; }
      public ICollection&lt;Student&gt; Students { get; set; }
  }
  </code></pre>