title: How to set up many to many relationship correctly with EF6?
body: <p>I have some issues with getting my many to many relationship to work. I have two tables, Beestje and Accessoire, and I want to show its contents.</p>
  
  <p>This are my tables:
  Beestje:</p>
  
  <pre><code>public class Beestje
  {
      public Beestje()
      {
          this.Accessoires = new HashSet&lt;Accessoire&gt;();
      }
  
      [Key]
      public int Id { get; set; }
  
      public String Naam { get; set; }
  
      public String Type { get; set; }
  
      public int Prijs { get; set; }
  
      public String Afbeelding { get; set; }
  
      public virtual ICollection&lt;Accessoire&gt; Accessoires { get; set; }
  }
  </code></pre>
  
  <p>Accessoire:</p>
  
  <pre><code>public class Accessoire
  {
      public Accessoire()
      {
          this.Beestjes = new HashSet&lt;Beestje&gt;();
      }
  
      [Key]
      public int Id { get; set; }
  
      public String Naam { get; set; }
  
      public double Prijs { get; set; }
  
      public String Afbeelding { get; set; }
  
      public virtual ICollection&lt;Beestje&gt; Beestjes { get; set; }
  }
  </code></pre>
  
  <p>In my controller I retrieve the contents from the Beestje table and return it to the view:</p>
  
  <pre><code>public ActionResult Beestjes()
      {
          ViewBag.Message = "Your application beestjes page";
  
          List&lt;Beestje&gt; beestjes;
  
          using (var context = new MyContext())
          {
              beestjes = context.Beestjes.ToList();
  
              return View(beestjes);
          }
      }
  </code></pre>
  
  <p>When I print it like this there are no problems:</p>
  
  <pre><code>@model IEnumerable&lt;BeestjeOpJeFeestje.Models.Beestje&gt;
  
  &lt;h2&gt;@ViewBag.Message&lt;/h2&gt;
  &lt;ul&gt;
      @foreach (var a in Model)
      {
          &lt;li&gt;@a.Naam&lt;/li&gt;
      }
  &lt;/ul&gt;
  </code></pre>
  
  <p>But when I print it like this I get an error:</p>
  
  <pre><code>@model IEnumerable&lt;BeestjeOpJeFeestje.Models.Beestje&gt;
  
  &lt;h2&gt;@ViewBag.Message&lt;/h2&gt;
  &lt;ul&gt;
      @foreach (var a in Model)
      {
          &lt;li&gt;@a.Naam&lt;/li&gt;
          foreach (var b in a.Accessoires)
          {
              &lt;li&gt;@b.Naam&lt;/li&gt;
          }
      }
  &lt;/ul&gt;
  </code></pre>
  
  <p>It goes wrong at the part where I call the a.Accessoires.
  The error is:
  "System.ObjectDisposedException: 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'"</p>
  
  <p>Does someone know the correct way, and would be willing to provide me the right code, or give me some directions?</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT:</p>
  
  <p>MyContext</p>
  
  <pre><code>public class MyContext : DbContext
      {
          public MyContext() : base("name=Local")
          {
              Database.SetInitializer(new MyContextInitializer());
          }
  
          public DbSet&lt;Beestje&gt; Beestjes { get; set; }
          public DbSet&lt;Accessoire&gt; Accessoires { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
          }
      }
  </code></pre>
  
  <p>MyContextInitializer</p>
  
  <pre><code>public class MyContextInitializer : DropCreateDatabaseAlways&lt;MyContext&gt;
      {
          protected override void Seed(MyContext context)
          {
              base.Seed(context);
  
              context.Beestjes.Add(new Beestje() { Id = 1, Naam = "Aap", Type = "Jungle", Prijs = 150, Afbeelding = "Content/images/aap.png" });
              context.Beestjes.Add(new Beestje() { Id = 2, Naam = "Olifant", Type = "Jungle", Prijs = 250, Afbeelding = "Content/images/olifant.png" });
              context.Beestjes.Add(new Beestje() { Id = 3, Naam = "Zebra", Type = "Jungle", Prijs = 200, Afbeelding = "Content/images/zebra.png" });
              context.Beestjes.Add(new Beestje() { Id = 4, Naam = "Leeuw", Type = "Jungle", Prijs = 250, Afbeelding = "Content/images/leeuw.png" });
              context.Beestjes.Add(new Beestje() { Id = 5, Naam = "Hond", Type = "Boerderij", Prijs = 75, Afbeelding = "Content/images/doggo.png" });
              context.Beestjes.Add(new Beestje() { Id = 6, Naam = "Ezel", Type = "Boerderij", Prijs = 150, Afbeelding = "Content/images/donkey.png" });
              context.Beestjes.Add(new Beestje() { Id = 7, Naam = "Koe", Type = "Boerderij", Prijs = 100, Afbeelding = "Content/images/koe.png" });
              context.Beestjes.Add(new Beestje() { Id = 8, Naam = "Eend", Type = "Boerderij", Prijs = 50, Afbeelding = "Content/images/duck.png" });
              context.Beestjes.Add(new Beestje() { Id = 9, Naam = "Kuiken", Type = "Jungle", Prijs = 35, Afbeelding = "Content/images/kuiken.png" });
              context.Beestjes.Add(new Beestje() { Id = 10, Naam = "Pinguin", Type = "Sneeuw", Prijs = 175, Afbeelding = "Content/images/pingwing.png" });
              context.Beestjes.Add(new Beestje() { Id = 11, Naam = "IJsbeer", Type = "Sneeuw", Prijs = 300, Afbeelding = "Content/images/ijsbeer.png" });
              context.Beestjes.Add(new Beestje() { Id = 12, Naam = "Zeehond", Type = "Sneeuw", Prijs = 200, Afbeelding = "Content/images/zeehond.png" });
              context.Beestjes.Add(new Beestje() { Id = 13, Naam = "Kameel", Type = "Woestijn", Prijs = 175, Afbeelding = "Content/images/kameel.png" });
              context.Beestjes.Add(new Beestje() { Id = 14, Naam = "Slang", Type = "Sneeuw", Prijs = 125, Afbeelding = "Content/images/slang.png" });
  
              var accessoire1 = new Accessoire() { Naam = "Banaan", Prijs = 0.50 };
              accessoire1.Beestjes.Add(context.Beestjes.Find(1));
              context.Accessoires.Add(accessoire1);
  
              var accessoire2 = new Accessoire() { Naam = "Zadel", Prijs = 4.50 };
              accessoire2.Beestjes.Add(context.Beestjes.Find(3));
              context.Accessoires.Add(accessoire2);
  
              var accessoire3 = new Accessoire() { Naam = "Krukje", Prijs = 5 };
              accessoire3.Beestjes.Add(context.Beestjes.Find(4));
              context.Accessoires.Add(accessoire3);
  
              var accessoire4 = new Accessoire() { Naam = "Zweep", Prijs = 2.50 };
              accessoire4.Beestjes.Add(context.Beestjes.Find(4));
              context.Accessoires.Add(accessoire4);
  
              var accessoire5 = new Accessoire() { Naam = "Bal", Prijs = 2.50 };
              accessoire5.Beestjes.Add(context.Beestjes.Find(5));
              accessoire5.Beestjes.Add(context.Beestjes.Find(12));
              context.Accessoires.Add(accessoire5);
  
              var accessoire6 = new Accessoire() { Naam = "Dansschoenen", Prijs = 3.00 };
              accessoire6.Beestjes.Add(context.Beestjes.Find(10));
              context.Accessoires.Add(accessoire6);
  
              context.SaveChanges();
          }
      }
  </code></pre>