While working ASP.NET MVC application using JSON serialization with Entity framework for database, sometimes we get a error in our Visual Studio saying 'A circular reference was detected while serializing an object of type System.Data.Entity.DynamicProxies', and we get stuck on it, so here i am going to explain brefily about it and will provide you a solution to avoid this error.

A circular reference was detected while serializing an object of type System.Data.Entity.DynamicProxies

Introduction With Example

First of all, this occurs because a parent object references a child object and this one reference back to its parents. Is it familiar? Well, if you are working with Entity Framework and Code First, it should. Why? Because most of the Entity are cross-referencing each others. This way, it’s convenient for many operations

For example: the below code may cause this issue

//fake method
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);
    }
}

You can build the solution and VS will successfully build it, you will not get the issue until you call it from front end side using some jQuery Ajax or Get call, for example the below code will throw the error

function getDetails() {
    $.get('/ControllerName/Details/' + Id, {}, function (data) {
        //do something with returned Data
    });
}

Now this code will throw the error "A circular reference was detected while serializing an object of type ‘object name".

The second problem is concerning the object itself. When trying to serialize, you are in fact serializing the Proxy also which is not what you want. In fact, you can get this error also, The RelationshipManager object could not be serialized. This type of object cannot be serialized when the RelationshipManager belongs to an entity object that does not implement IEntityWithRelationships.

Solutions

You should disable the proxy creation and handle the state of your poco with the DbContext instead of relying on proxy. You can disable the proxy by setting it to false  in your DbContext’s property ProxyCreationEnabled, like this:

dbContext.Configuration.ProxyCreationEnabled = false;

Now our EF Code First setup works! But "When proxy object creation is enabled for POCO entities, changes that are made to the graph and the property values of objects are tracked automatically by the Entity Framework as they occur. For information about change tracking options with and without proxies, see Tracking Changes in POCO Entities."

If it is fine for you, then you are done with the error.

Second way, is to broke those references when we send the information to JSON. That mean that in your controller, before sending back the ActionResult, you loop problematic property to remove the reference. 

Or you can say 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);
  }
}

Third solution can be to use the attribute ScriptIgnore, So, it will serialize only from one side, you can take a look at this issue with example here

A better solution is to use a different library of Json which can handle reference, by that I mean the Newton King Library. This library support circular reference by add ID to object serialized.

For this, you need to remove the serialization from the default JSON serializer and use the Json Newton King Library. This can be done in the Global.asax.cs file. with the following code.

lobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonNetFormatter(new JsonSerializerSettings()));
 
var jsonSerializerSettings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
};
//Source http://patrickdesjardins.com/blog/circular-reference-with-entity-framework-and-json-when-developing-with-asp-net-mvc

As you can see, we are setting up the PreserceReferencesHandling.Objects. This will add the reference to object when circular dependencies are found.

This setting adds values into the JSON structure that in the format “$id”: “1” and instead of copying the object over and over (and creating a stack over flow), will use the syntax “$ref”: “1” to refer to the object.

Conclusion, we have to remember two things with Asp.Net MVC and Entity Framework.

First, if you have not disabled proxy, than you will have this object serialized and it will cause problem when serializing. Second, you must break those circular reference to stop the possibility of stack overflow. Using JSON Newton library is simple and powerful when it came to handling references.

In Code first approach, you should also remove the virtual before each collection of your model.