Serializing XML Entity Objects

I am trying to serialize and graph EF 4.0 objects in XML for transmission through the WCF service. I used to do this with DTO / POCO (usually for JSON serialization). In this case, since I only perform XML serialization, it seemed to me that I could just serialize entity objects, but I ran into this puzzle:

  • If I do not detach the object, serialization throws an error that the object context has been deleted (because it has at this point that this is expected).

  • If I detach an object, all related objects loaded in the navigation properties will be deleted.

My assumption was that if I enumerated any related objects, then separated the object from the context, I would still have these relationships available for serialization.

So my question is: is there any way to serialize an entity object and save any loaded navigation properties / collections?

Thank...

+3
source share
1 answer

When serializing an object, the serializer will process the entire graph of objects.

  • If your object is connected, it will force-load all lazy loads. That way, if your context is located, you will get an exception.
  • According to msdn , when an item in the navigation properties is disabled, it is no longer displayed in the navigation property. I think this is the same when detaching an object and accessing the navigation property.

, DTO/POCO . automapper, DTO .

+1

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


All Articles