Parent-relationship entities are not serialized? RRS feed

  • Question

  • Hi all


    I'm retrieving a LINQ entity in my middle tier, and using LoadOptions to pre-fetch entities that exist in various parent and child relationships to that entity. After retrieving the entity in this tier I can see that all the parent and child entities are present in the graph. However when I pass the entity back up to my UI (via WCF), I find that only the child collections have made it - all the parent entities are null. Looking at the generated entity code these parent entity properties aren't marked with the [DataMember] attribute, i.e. they don't get serialized (anyone know why MS made the decision not to do this?).


    Short of manually changing these generated classes (which I *really* don't want to do), is there any other way to get the parent entities back to the UI? Other than a separate call to the BLL of course...


    Thanks in advance




    Monday, June 16, 2008 3:01 PM


  • Hi there,


    Not marking the parents as DataMember is a correct design decision, as WCF's data contract serializer will complain about circular reference (while serializing parent, you'll need to serialize the children, then for each child, serialize parent again...)


    One work around is: Create another field such as Parent_back in a partial class and assign it manually. Then mark parent_back as serializable. This works fine for me currently.


    Hope this helps.




    Monday, June 16, 2008 5:29 PM