title: EF5 with Many to Many Relationship creates cyclic reference when serializing to JSON
body: <p>I have an Entity Framework 5 Code First model with a many to many relationship
  i.e.</p>
  
  <pre><code>Public class Product
  {
      public int ProductId {get;set;}
      public ICollection&lt;Category&gt; Categories {get;set;}
  }
  
  Public class Category
  {
      public int CategoryId {get;set;}
      public ICollection&lt;Product&gt; Products {get;set;}
  }
  </code></pre>
  
  <p>I'm creating the actual relation in fluent, thus;</p>
  
  <pre><code>modelBuilder.Entity&lt;Product&gt;()
              .HasMany(p =&gt; p.Categories)
              .WithMany(c =&gt; c.Products)
              .Map(m =&gt;
                  {
                      m.ToTable("ProductsToCategories");
                      m.MapLeftKey("Products_ProductId");
                      m.MapRightKey("ProductCategories_ProductCategoryId");
                  });
  </code></pre>
  
  <p>Now when I retrieve my data is is retrieving Product, and Product as a bunch of Categories, BUT each Category also has a bunch of products in it as well, and so it recurses around.</p>
  
  <p>The problem is that this is causing havoc when I then serialise it to JSON for use by the front end (I'm using KnockOut, but that is kind of irrelevant).</p>
  
  <p>I've tried turning Lazy loading off, and when I get my products I use an include;</p>
  
  <pre><code>db.Products.Include("Categories").ToList()
  </code></pre>
  
  <p>but this is still then performing the recursive gathering of products within each Category.</p>
  
  <p>Any ideas?</p>
  
  <p>Regards</p>