I use the library (Telerik) for ASP.NET MVC 3.
I have a problem with all the functions that need to return AJAX for some data:
I use EF4 to connect my database, and I (and need) the navigation properties in both directions (Imagine, a user who has several messages and one mail, has a user).
The problem is that the library uses JavaScriptSerializer, which encodes data using JSON.
The error I get
A circular reference was found while serializing an object of type "System.Data.Entity.DynamicProxies.Employee_34048F4F6A98297F826C798A27640C7383E95E8EA8282EC8A5F738FA0C77CBC3".
An exception that is correct, because the parent has a link to his children, and the children have a link to his parent.
I already found some workaround, but nothing that completely satisfies me:
- Use data server binding: impossible, the functional function should work in ajax (its pager for the grid, which loads the following elements when the page scrolls to the end)
- Using anonymous objects: not flexible, because if I have a field in the database, I have to add it to my entire anonymous object, and besides, if I need to have the sub-series that I want, it is boring to also create objects for the entire element of this sublist.
- Use ViewModel: Almost the same problem, if I have another field, I have to add these fields to all viewModel, and I need to create a view model for all my 60 views with the same fields as my model ...
- Use NonSerializedAttribute: I donβt know how to place my T4 template that generates my POCO objects, and Iβm not sure that it will work: once the main object is children, sometimes itβs the parent, the empty relationship should be another.
I had almost the same problem with WCF and I created a serialized one that knows how to handle a circular link, can we do the same here? Or is there a way to handle this?
If not, I suppose the best way is to use the "ViewModel", but is there a way to speed up this creation? How is a generic object that takes an EF object in the constructor and removes the circular reference? anything else?
Thank you very much
source share