Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework and DataContractSerializer

I've been reading about serialization of entities graph in an entity framework context using Linq to entities and the different possible serializers: Binary, XmlSerializer and DataContractSerializer. As i understood the binary and XmlSerializer serialize the entity without its relationships. In case relationships are serialized it would cause a problem because of the nature of the resulting xml file structure ( for XmlSerializer). The DataContractSerializer serialize the graph in its whole depth unless the lazy load is disabled.

My question is: I want to serialize a part of the graph. For example if I have an entity A and three related entities B, C and D, only B and D would be serialized with A. I want to use the the DataContractSerializer. If I delete the [DataMemberAttribute] of the unwanted navigational properties would that work?

like image 738
Rahma Avatar asked Mar 14 '26 13:03

Rahma


1 Answers

You can actually disable lazy-loading, serialize/deserialize, and then re-enable lazy-loading.

context.ContextOptions.LazyLoadingEnabled = false;

StackOverflow Source

like image 129
Alec Avatar answered Mar 17 '26 02:03

Alec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!