title: Mapping many to many relationship in entity framework code first
body: <p>I'm try make a test in EF, creating a many to many relationship, because I always mapping One to One or One to Many, I has get a example in the internet for try, the example is working for insert registers, but I can't read registers</p>
  
  <p>Here is my classes, I don't know what is <code>HashSet</code>, I get this code in the site</p>
  
  <pre><code>public class Person
  {
      public int PersonId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  
      public ICollection&lt;Course&gt; CoursesAttending { get; set; }
  
      public Person()
      {
          CoursesAttending = new HashSet&lt;Course&gt;();
      }
  }
  
  public class Course
  {
      public int CourseId { get; set; }
      public string Title { get; set; }
  
      public ICollection&lt;Person&gt; Students { get; set; }
  
      public Course()
      {
          Students = new HashSet&lt;Person&gt;();
      }
  }
  </code></pre>
  
  <p>Here is my Context</p>
  
  <pre><code>public class SchoolContext : DbContext
  {
      public DbSet&lt;Course&gt; Courses { get; set; }
      public DbSet&lt;Person&gt; People { get; set; }
  
      public SchoolContext()
          : base("MyDb")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Course&gt;().
              HasMany(c =&gt; c.Students).
              WithMany(p =&gt; p.CoursesAttending).
              Map(
                  m =&gt;
                  {
                      m.MapLeftKey("CourseId");
                      m.MapRightKey("PersonId");
                      m.ToTable("PersonCourses");
                  });
  
      }
  }
  </code></pre>
  
  <p>When I insert registers is right</p>
  
  <pre><code>static void Main(string[] args)
  {
      using (SchoolContext db = new SchoolContext())
      {
          Course math = new Course();
          Course history = new Course();
          Course biology = new Course();
          math.Title = "Math";
          history.Title = "History";
          biology.Title = "Biology";
  
          db.Courses.Add(math);
          db.Courses.Add(history);
          db.Courses.Add(biology);
  
          Person john = new Person();
          john.FirstName = "John";
          john.LastName = "Paul";
          john.CoursesAttending.Add(history);
          john.CoursesAttending.Add(biology);
  
          db.People.Add(john);
          db.SaveChanges();
      }
  }
  </code></pre>
  
  <p>But when I try select register for show content, is not work, it just print nothing</p>
  
  <pre><code>static void Main(string[] args)
  {
      using (SchoolContext db = new SchoolContext())
      {
          Pearson p = db.Peasons.First();
          Console.WriteLine(p.CoursesAttending.First().Title);
      }
  }
  </code></pre>
  
  <p>I had check in the database, registers exist, what is the problem? </p>
  
  <p>Please teach me how to make select in many to many relationship with code first.</p>