title: Serialize EF Object into JSON - ASP.NET MVC
body: <p>I have an EF Object:</p>
  
  <pre><code>public class User
  {
  
      [Key, Column("userid", TypeName = "int")]
      public Int32 UserId { get; set; }
  
      [Column("username", TypeName = "varchar")]
      public String UserName { get; set; }
  
      [Column("password", TypeName = "varchar")]
      public String Password { get; set; }
  
      [Column("name", TypeName = "varchar")]
      public String Name { get; set; }
  
      [Column("surname", TypeName = "varchar")]
      public String Surname { get; set; }
  
      [Column("email", TypeName = "varchar")]
      public String Email { get; set; }
  
      [Column("dob", TypeName = "datetime")]
      public Nullable&lt;DateTime&gt; Dob { get; set; }
  
      [Column("notes", TypeName = "nvarchar")]
      public String Notes { get; set; }
  
      [Column("masterentity", TypeName = "varchar")]
      public String MasterEntity { get; set; }
  
      [Column("propertyid", TypeName = "int")]
      public Nullable&lt;Int32&gt; PropertyId { get; set; }
  
      [Column("boardmember", TypeName = "bit")]
      public Boolean BoardMember { get; set; }
  
      [Column("occupiesunit", TypeName = "bit")]
      public Boolean OccupiesUnit { get; set; }
  
      [Column("systemuser", TypeName = "bit")]
      public Boolean SystemUser { get; set; }
  
      [Column("isactive", TypeName = "bit")]
      public Boolean IsActive { get; set; }
  
  
      #region Foreing Keys
  
      [ForeignKey("MasterEntity")]
      public virtual Entity CurrentMasterEntity { get; set; }
  
      #endregion
  
  }
  </code></pre>
  
  <p>In client side Im trying to serialize the model into a JSON object like this:</p>
  
  <pre><code>var jsonUser = @(Html.Raw(Json.Encode(this.Model))); 
  </code></pre>
  
  <p>Im getting the following error:</p>
  
  <pre><code>A circular reference was detected while serializing an object of type ....
  </code></pre>
  
  <p>What I realize is that if I <strong>remove</strong> the Foreing Keys Fluent API  </p>
  
  <pre><code>[ForeignKey("MasterEntity")]
  public virtual Entity CurrentMasterEntity { get; set; }
  </code></pre>
  
  <p>Then it works perfect. So seems that the Entities or Objects that has relations with other Entity cant be serialized using JSON.</p>
  
  <p>Anyone has a good approach of solving this? IS EF 5.0 going to solve this issue?</p>
  
  <p>Thanks a lot.</p>