title: Serializing Entity Framework Objects into JSON
body: <pre><code>public class GenericHandler : IHttpHandler
  {
      public class ASSystem
      {
          public string SID { get; set; }
          public string Description { get; set; }
          public string SystemName { get; set; }
      }
  
      public class ErrorObj
      {
          public string ErrorMessage { get; set; }
      }
  
      public void ProcessRequest(HttpContext context)
      {
          HttpContext.Current.Response.ContentType = "application/json";
          HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
  
          string query = HttpContext.Current.Request.QueryString["SID"];
  
  
          SOFAEntities ctx = new SOFAEntities();
          JavaScriptSerializer serializer = new JavaScriptSerializer();
  
          try
          {
              AS_SYSTEM system = ctx.AS_SYSTEM.Where(s =&gt; s.SYSTEM_ID == query).First() as AS_SYSTEM;
  
              if (system != null)
              {
                  ASSystem sys = new ASSystem() { SID = system.SYSTEM_ID, Description = system.DESCRIPTION, SystemName = system.SYSTEM_NAME };
                  HttpContext.Current.Response.Write(serializer.Serialize(sys));
              }
          }
          catch (Exception e)
          {
              HttpContext.Current.Response.Write(serializer.Serialize(new ErrorObj() { ErrorMessage = e.Message }));
          }
  
  
  
  
  
      }
  
      public bool IsReusable
      {
          get
          {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>This works, but when i try with <code>HttpContext.Current.Response.Write(serializer.Serialize(system));</code> i get the following error:</p>
  
  <blockquote>
    <p>A circular reference was detected while serializing an object of type
    'System.Data.Metadata.Edm.AssociationType</p>
  </blockquote>
  
  <p>What i wanted was a json object representating the complete as_system object, so i dont have to map each property manually. Is there any way to solve this? Thanks!</p>