title: Entity Framework Circular Reference
body: <p>Trying this question again because my first attempt was barely coherent :p</p>
  
  <p>So I am super confused and using Entity Framework Code First</p>
  
  <p>I have a Forest class.</p>
  
  <p>I have a Tree class.</p>
  
  <p>Each Forest can have many Trees</p>
  
  <p>When I was trying to serialize I was getting circular reference </p>
  
  <pre><code>public class Forest
  {
  
      public Guid ID { get; set; }  
      public virtual List&lt;Tree&gt; Trees { get; set; }
  }
  public class Tree
  {
      public Guid ID { get; set; }
      public Guid? ForestId {get;set;}
  
      [ForeignKey("ForestId")]
      public virtual Forest Forest {get;set;}
   }
  </code></pre>
  
  <p>Every forest has trees but not every tree is in a forest.  I struggle with either errors of Multiplicity when doing</p>
  
  <pre><code>@(Html.Raw(Json.Encode(Model)))
  </code></pre>
  
  <p>Where the model is a Forest</p>
  
  <p>and if I make <code>ForestId</code> a <code>Guid</code> instead of a <code>Guid?</code> I get Circular Reference errors.</p>
  
  <p>I also tried 
  protected override void </p>
  
  <pre><code>OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) 
  { 
    modelBuilder.Entity&lt;Forest&gt;() 
    .HasMany(x =&gt; x.Tree) 
    .WithOptional() 
     .HasForeignKey(y =&gt; y.ForestId); 
  }
  </code></pre>
  
  <p>Thanks in advance</p>