Click here to Skip to main content
Click here to Skip to main content

Tagged as

Avoiding Circular Reference for Entity in JSON Serialization

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Avoiding Circular Reference for Entity in JSON Serialization

Introduction

One problem that I was facing yesterday while working on an ASP.NET MVC application was a JSON serialization issue. The problem was a circular reference caused by the DataContractJsonSerializer because of relations between the entity and other entities. In this post, I’ll show you how you can use a simple workaround in order to avoid the problem.

The JSON Serialization Error

Returning the output of the JSON method in an ASP.NET MVC controller with a JSON object graph based on an Entity Framework Code First object causes the following error – “A circular reference was detected while serializing an object of type ‘object name’”.

Here is an example of a method that might cause the problem:

public ActionResult Details(int id)
    using (var context = new DbEntities())
    {             
        var entity = context.Entities.
          Include(e => e.OtherEntities).
          FirstOrDefault(e => e.EntityId == id);
        return Json(entity, JsonRequestBehavior.AllowGet);
    }
}

Pay attention that this is a fake method and isn’t a real method.

The error I was getting didn’t show up on the server side (nothing crashed). I found the error by inspecting Fiddler since the application kept on working but the client side callback wasn’t. Here is an example of the calling client side function which is using jQuery getJSON function:

function getDetails() {
    $.getJSON('/ApplicationName/Details/' + Id, {}, function (data) {
        fillDetails(data);
    });
}

The circular reference is a known problem that my colleague Ido wrote about in Microsoft Connect.
So how to avoid it?

The Workaround

Use a data projection to project only the relevant data that you need and send this data to the client side. This workaround will minimize the object that you are sending and also play a role as a ViewModel kind of object instead of an entity when it is available on the client side. Here is an example of using the workaround with the previous method:

public ActionResult Details(int id)
{
  using (var context = new DbEntities())
  {
    var entity = context.Entities.
      Include(g => g.OtherEntities).
      Select(e => new { 
        e.Id,
        e.Url,
        e.Name,
        e.OtherEntities
      }).
      FirstOrDefault(e => e.Id == id);
 
    return Json(entity, JsonRequestBehavior.AllowGet);
  }
}

Summary

The circular reference issue might happen also in WCF script services or in other places while serializing Entity Framework’s entities into JSON representation. The simple workaround is to create a projection of the data that you need and serialize it into JSON representation. One of the gains of this method is minimizing the response size from the server.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Questionthanks Pinmemberamrm2e27-Mar-14 1:08 
AnswerRe: thanks PinpremiumGil Fink12-Apr-14 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid