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.

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

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

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:

As you can see, we are setting up the PreserceReferencesHandling.Ojects. 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.