Json.Net is used to serialize Entity Framework objects.

asp.net-mvc entity-framework javascript json.net


How can I convert entity framework objects into JSON, the JavaScript Object Nucleus? I tried using JSON.NET, but when I attempt to serialize it, I get the following issue.

Newtonsoft.Json.JsonSerialization is an exception. Exception, "Self referencing loop" in the message


6/24/2009 4:20:49 AM

Accepted Answer

Regarding cyclic references, it seems that you are experiencing the same basic issue as the original DataContract serializer. Even while objects referring one another in object graphs in memory is rather typical, if the serializer does not particularly take into account for it, such cyclic references eventually lead to endless recursions when serialized. Cyclic references are a problem that is seldom, if ever, addressed by established standards in the popular non-binary serialization formats (XML and JSON being the two most prevalent.)

Microsoft used the xml ref semantics to resolve the cyclic issue with the DataContract serializer in.NET 3.5 SP1. As far as I'm aware, JSON doesn't support this, which might be the reason JSON.NET is prohibiting you from serializing your object graph.

I would make sure that your object graph only contains references that may be navigated in one direction, rather than both directions (i.e. only from parent to child, not from child to parent.) The most typical cyclic reference types are parent/child and child/parent. Another possibility is that a lower-level child is eventually accessing the graph's root, which would result in an indirect cyclic graph (albeit these are typically far less frequent than parent/child loops).

After removing any cyclic references from your object graph, serialization ought to be possible.

6/24/2009 4:29:46 AM

Popular Answer

I ran across similar issue and added Newtonsoft to fix it. Json. To avoid the loop, apply JsonIgnoreAttribute to the property. That property won't be serialized, of course. I often include both the foreign reference ID and the foreign class in my entities to aid with this issue. I am aware that this approach is not very natural or excellent in terms of OO, however Julia Lerman suggests it in her book Programming Entity Framework: Code First. It seems to assist with a number of Entity Framework issues.

 public class SomeEntity
      public ForeignEntity SomeForeignEntity {get;set;}
      public Guid ForeignEntityId {get;set;}

Update: I neglected to note that I also had to deactivate proxies on the DbContext in the following manner:

dataContext.Configuration.ProxyCreationEnabled = false;

There are certain things you lose when the proxy construction is disabled, but if you are building the code for a service (which seems probable if you are serializing), then this is probably not a concern. See http://www.sellsbrothers.com/posts/Details/12665 for further information.

Since I'm utilizing the MS Web Api, I just turn off proxy generation while building my controller:

public class MailingApiController : ApiController
    public MailingApiController()

    private static void PreventDeepSerialization()
        var dataContext = Injector.Get<IIntertwyneDbContext>();
        dataContext.Configuration.ProxyCreationEnabled = false;

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow