How to serialize an EF POCO auto-generated object to avoid circular references?

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

+4
source share
1 answer

Actually, not [NonSerialized] , you need [ScriptIgnore] . I would approach this from one of two angles:

  • use RegisterConverters and write a custom converter that sets all the properties except the parent one (maybe it’s also possible to automate it, maybe using the class-level attribute, for example [SkipSerialize("Parent")] added to the partial class , which is probably complicates the situation too much.)

  • simple: do not serialize EF POCO, but use DTO instead - this seems to be what you mean in the ViewModel answer. Personally, I have no problems with the fact that I have a version of the class (EF / POCO) and very similar, but another version of DTO - their intent is different, and, in my opinion, this does not violate DRY.

+5
source

Source: https://habr.com/ru/post/1386258/


All Articles