title: EntityFramework to Json workaround? ( A circular reference was detected while serializing an object of type...DynamicProxies)
body: <p>so heres the deal i have </p>
  
  <p><strong>Models</strong></p>
  
  <pre><code>public class News
  {
  
      public News()
      {
          this.Created = DateTime.Now;
      }
  
      public int Id { get; set; }       
      public string Title { get; set; }
      public string Preamble { get; set; }
      public string Body { get; set; }
      public DateTime Created { get; set; }
  
      public int UserId { get; set; }
  
      public virtual User User { get; set; }
  
      public int CategoryId { get; set; }
      public int ImageId { get; set; }
  
      public virtual Image Image { get; set; }
      public virtual Category Category { get; set; }
  }
  
  public class Image
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string ImageUrl { get; set; }
      public Byte[] ImageData { get; set; }
      public string ImageMimeType { get; set; }
  }
  
  public class Category
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>....following models(these models are connected to the EfDbContext) connected to  following repository...</p>
  
  <p><strong>Interface/Repository</strong></p>
  
  <pre><code>public class NewsRepository : INewsRepository
  {
      EfDbContext context = new EfDbContext();
  
      public IQueryable&lt;News&gt; All
      {
          get { return context.News; }
      }
  
      public IQueryable&lt;News&gt; AllIncluding(params Expression&lt;Func&lt;News, object&gt;&gt;[] includeProperties)
      {
          IQueryable&lt;News&gt; query = context.News;
          foreach (var includeProperty in includeProperties) {
              query = query.Include(includeProperty);
          }
          return query;
      }
  
      public News Find(int id)
      {
          return context.News.Find(id);
      }
  
      public void InsertOrUpdate(News news)
      {
          if (news.Id == default(int)) {
              // New entity
              context.News.Add(news);
          } else {
              // Existing entity
              context.Entry(news).State = EntityState.Modified;
          }
      }
  
      public void Delete(int id)
      {
          var news = context.News.Find(id);
          context.News.Remove(news);
      }
  
      public void Save()
      {
          context.SaveChanges();
      }
  }
  
  public interface INewsRepository
  {
      IQueryable&lt;News&gt; All { get; }
      IQueryable&lt;News&gt; AllIncluding(params Expression&lt;Func&lt;News, object&gt;&gt;[] includeProperties);
      News Find(int id);
      void InsertOrUpdate(News news);
      void Delete(int id);
      void Save();
  }
  </code></pre>
  
  <p>In my HomeController() i got a a JsonResult metod that i want to return the context.
  Here is the Method</p>
  
  <p><strong>Json Request</strong></p>
  
  <pre><code>    [HttpGet]
      public JsonResult GetNews()
      {
          var p = newsRepository.AllIncluding(news =&gt; news.Category, news =&gt; news.Image);
          return Json(p, JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <p><strong>A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.News_96C0B16EC4AC46070505EEC7537EF3C68EE6CE5FC3C7D8EBB793B2CF9BD391B3'.</strong></p>
  
  <p>I guessed that this has something to do with the lazyloading stuff(Iam currently learning about C#) i found this article about this...</p>
  
  <p><a href="http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/">http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/</a></p>
  
  <p>but i didnt get it to work... what i could read about the code was that they were tryin to depth search trough the object... more than that i couldn't figure out.</p>
  
  <p>my question is how to i can pass in lazyLoading objects? into json/serializer 
  or does it not exist, any thoughts of how i can proceed?</p>