Click here to Skip to main content
15,867,453 members
Articles / All Topics

Avoiding Circular Reference for Entity in JSON Serialization

Rate me:
Please Sign up or sign in to vote.
4.78/5 (3 votes)
1 Dec 2011CPOL2 min read 55.9K   4   2
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:

C#
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:

JavaScript
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:

C#
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.


This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

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


Written By
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

Comments and Discussions

 
Questionthanks Pin
elramsisy27-Mar-14 1:08
elramsisy27-Mar-14 1:08 
AnswerRe: thanks Pin
Gil Fink12-Apr-14 0:29
Gil Fink12-Apr-14 0:29 

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

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