title: EF 4.1 - Code First - JSON Circular Reference Serialization Error
body: <p>I am getting an a Circular Reference Serialization Error although, to my knowledge I do not have any circular references. I am retrieving a set of Orders from the database and sending them to the client as JSON. All the code is shown below.</p>
  
  <p>This is the error:</p>
  
  <blockquote>
    <p><strong>Error</strong></p>
    
    <p>A circular reference was detected
    while serializing an object of type
    'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.
    Description: An unhandled exception
    occurred during the execution of the
    current web request. Please review the
    stack trace for more information about
    the error and where it originated in
    the code. </p>
    
    <p>Exception Details:
    System.InvalidOperationException: A
    circular reference was detected while
    serializing an object of type
    'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.</p>
    
    <p>Source Error: </p>
    
    <p>An unhandled exception was generated
    during the execution of the current
    web request. Information regarding the
    origin and location of the exception
    can be identified using the exception
    stack trace below.</p>
  </blockquote>
  
  <p>My classes are as follows:</p>
  
  <p><strong>Order</strong></p>
  
  <pre><code>public class Order
  {
      [Key]
      public int OrderId { get; set; }
  
      public int PatientId { get; set; }
      public virtual Patient Patient { get; set; }
  
      public int CertificationPeriodId { get; set; }
      public virtual CertificationPeriod CertificationPeriod { get; set; }
  
      public int AgencyId { get; set; }
      public virtual Agency Agency { get; set; }
  
      public int PrimaryDiagnosisId { get; set; }
      public virtual Diagnosis PrimaryDiagnosis { get; set; }
  
      public int ApprovalStatusId { get; set; }
      public virtual OrderApprovalStatus ApprovalStatus { get; set; }
  
      public int ApproverId { get; set; }
      public virtual User Approver { get; set; }
  
      public int SubmitterId { get; set; }
      public virtual User Submitter { get; set; }
  
      public DateTime ApprovalDate { get; set; }
  
      public DateTime SubmittedDate { get; set; }
      public Boolean IsDeprecated { get; set; }
  }
  </code></pre>
  
  <p><strong>Patient</strong></p>
  
  <pre><code>public class Patient
  {
      [Key]
      public int PatientId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string MiddleInitial { get; set; }
      public bool IsMale;
      public DateTime DateOfBirth { get; set; }
  
      public int PatientAddressId { get; set; }
      public Address PatientAddress { get; set; }
  
      public bool IsDeprecated { get; set; }
  }
  </code></pre>
  
  <p><strong>Certification Period</strong></p>
  
  <pre><code>public class CertificationPeriod
  {
      [Key]
      public int CertificationPeriodId { get; set; }
      public DateTime startDate { get; set; }
      public DateTime endDate { get; set; }
      public bool isDeprecated { get; set; }
  }
  </code></pre>
  
  <p><strong>Agency</strong></p>
  
  <pre><code>public class Agency
  {
      [Key]
      public int AgencyId { get; set; }
      public string Name { get; set; }
  
      public int PatientAddressId { get; set; }
      public virtual Address Address { get; set; }
  }
  </code></pre>
  
  <p><strong>Diagnosis</strong></p>
  
  <pre><code>public class Diagnosis
  {
      [Key]
      public int DiagnosisId { get; set; }
      public string Icd9Code { get; set; }
      public string Description { get; set; }
      public DateTime DateOfDiagnosis { get; set; }
      public string Onset { get; set; }
      public string Details { get; set; }
  }
  </code></pre>
  
  <p><strong>OrderApprovalStatus</strong></p>
  
  <pre><code>public class OrderApprovalStatus
  {
      [Key]
      public int OrderApprovalStatusId { get; set; }
      public string Status { get; set; }
  }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>public class User
  {
      [Key]
      public int UserId { get; set; }
      public string Login { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string NPI { get; set; }
      public string Email { get; set; }
  
  }
  </code></pre>
  
  <blockquote>
    <p><strong>NOTE: ADDRESS CLASS IS NEW ADDITION DURING EDIT</strong></p>
  </blockquote>
  
  <p><strong>Address</strong></p>
  
  <pre><code>public class Address
  {
      [Key]
      public int AddressId { get; set; }
      public string StreetAddress { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public string Zip { get; set; }
      public string Phone { get; set; }
      public string Title { get; set; }
      public string Label { get; set; }
  }
  </code></pre>
  
  <p>The code that executes the serialization is here:</p>
  
  <p><strong>Excerpt from OrderController</strong></p>
  
  <pre><code>    public ActionResult GetAll()
      {
          return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p>Thanks</p>