Avoiding circular reference when serializing an Entity Framework class

I have an MVC-3 (RC1) application using Entity Framework 4.

I want to return a JSON object from a controller action. This object is referenced by other objects that explicitly return the link.

Thus, I get the following circular reference error:

Server error in application "/".

A circular reference was detected while serializing an object of type 'Application.Models.ReferenceObject'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it occurred in the code.

Exception Details: System.InvalidOperationException: A circular reference was detected during serialization of an object of type 'Application.Models.ReferenceObject'.

NB: The application and ReferenceObject explicitly replace the actual name / object space.

According to Stack Overflow: the exception of circular references when serializing LINQ to SQL classes , this can be overcome using JSON.Net; however, I would like to avoid this and instead try to exclude offensive reference properties from the serializable object.

What I mean?

I want to do something like this:

IList<ReferenceObject> list = Repository.GetReferenceObjects(); return Json(list.**<method>**("ObjectsReferencingThis")); 

where **<method>** is some method opposite ObjectQuery(Of T).Include and ObjectsReferencingThis is a property that causes a circular reference.

NB: I do not want to remove these properties or create POCO, as this only affects Json serialization.

Can anybody help?

:)

+1
asp.net-mvc-3 entity-framework-4
Dec 14 '10 at 5:59
source share
2 answers

I had a similar problem when I was working on one of my previous projects. Here is what I did:

 IList<Product> list = Repository.GetProducts(); var collection = products.Select(product => new { id = product.Id, name = product.Name, detailUrl = product.DetailUrl, imageLargeUrl = product.ThumbNailUrl, tagtitle = product.Name.ToUpper(), tagheader = "Words our cherished patrons use to describe this product", tagwords = from tag in product.Tags group tag by tag.Name into words select new { name = words.Key, weight = words.Count() } }); var result = new {id = inquiry.Id, products = collection, }; return this.Jsonp(result); 

Here is what the Json result looks like:

 { "id" : 2, "products" : [{ "id" : "3605970008857", "name" : "TITLE1", "detailUrl" : "http://www.urlhere.com", "tagwords" : [{ "name" : "roses", "weight" : 1 }, { "name" : "cotton", "weight" : 1 }, { "name" : "happy", "weight" : 1 }] }, { "id" : "3605970019891", "name" : "TITLE2", "detailUrl" : "http://www.urlhere.com", "tagwords" : [] }], 

}

You can also add any other properties from the referenced objects to the result, as you want, to show it in a Json object :)

+2
Dec 14 '10 at 6:29
source share

I made a very trivial decision, which is not recommended if you have a very large list

 letters=UserOperations.GetDepartmentLettersForSecretary(pageNumber, pageSize,(Session["User"] as User).DepartmentID.Value, (Session["User"] as User).ID); foreach (Letter letter in letters) { letter.LetterStatus.Letters = null; } 

the circular reference problem in my case is in LetterStatus.Letters so I Iterated through the list and assigned it to null

as i told you its not recommended if you have a very big list

0
Sep 26 '16 at 17:40
source share



All Articles