1

I am not able to serialize nested entities.

This is my transfer object

[ProtoContract]
[Serializable]
[DataContract]
public class MyClassTO
{
    [ProtoMember(1)]
    [DataMember]
    public List<MyEntity> ListOfMyEntities {get;set;};

}

On my backend, I fire the following linq to entities query, fill the transfer object and return it to the client:

var myClassTO  = new MyClassTO();

myClassTO.ListOfMyEntities = Context.MyEntity.Include("ReferencedEntity.AnotherReferencedEntity").ToList;

return myClassTO;

The client receives the list of MyEntity objects but included entities are not serialized(they are all null). Does anybody have any idea about it?

ADE
  • 31
  • 3
  • Can I clarify, then: have you checked that `ListOfMyEntities` has contents before serialization? Also: if this is EF POCO, try making `ListOfMyEntities` a `virtual` member (i.e. add the `virtual` modifier). – Marc Gravell May 31 '12 at 10:29
  • Hi Marc, thanks for the quick answer. The ListOfMyEntities property has contents and the client receives it. In other words, the serialization of the list works, but the property ReferencedEntity of the MyEntity objects of the list is always all null. No, this is not EF POCO. – ADE May 31 '12 at 10:44
  • k; what does `MyEntity` look like? also: what is the process here? are you serializing to disk and loading from disk? or is this WCF? or...? – Marc Gravell May 31 '12 at 10:46
  • Sorry, forgot to mention that, WCF. MyEntity (and ReferencedEntity as well) is a class automatically generated by the EF designer based on the schema of my database. – ADE May 31 '12 at 10:50
  • k; any chance I can see both client and server parts of any random property (your choice)? I'm guessing the numbers are off-by-one (which, if so, is easily fixable - but I need to see the problem first). For example, it might be `[DataMember(4)] public string Name {get;set;}` at the server, and `[DataMember(3)] public string Name {get;set;}` at the client. (also: don't start changing the numbers! there's a trick here...) – Marc Gravell May 31 '12 at 10:57
  • Maybe I have found the reason. The EF Designer decorates relationshinp navigation properties with the attribute DataMemberAttribute but it does not define any order. Could this be the reason why protobuf ignores the property? [EdmRelationshipNavigationPropertyAttribute("MyModel", "FK_MyEntity_OBJECTID", "ReferencedEntity")] [XmlIgnoreAttribute()] [SoapIgnoreAttribute()] [DataMemberAttribute()] public ReferencedEntity ReferencedEntity {... – ADE May 31 '12 at 11:53
  • that's odd; but we can add attributes to a partial class to fix it - ProtoPartialMember is the easiest route. It needs these numbers at both ends :( – Marc Gravell May 31 '12 at 12:36
  • or a simpler answer: send DTOs ;p – Marc Gravell May 31 '12 at 12:41
  • You are suggesting to add the [ProtoPartialMember] attribute to the property ReferencedEntity, am I right? – ADE May 31 '12 at 12:46
  • ProtoPartialAttribute is added to the *class*, **indicating** that a named (string) member should act as though it has the attribute. The handy thing here is that you can add it to a partial class, and thus avoid having to edit a generated file. – Marc Gravell May 31 '12 at 12:48
  • I have added the attribute to MyEntity class and now upon serializing the object I get this exception "Possible recursion detected (offset: 4 level(s)): MyEntity" – ADE May 31 '12 at 13:13
  • is there a `.Parent` property or something like that? protobuf-net is *intended* to be used as a tree serializer (not a graph serializer). It *can* operate as either, but it prefers simply not to serialize `.Parent` properties – Marc Gravell May 31 '12 at 13:49
  • No idea, I should deeper dig into the auto generated code. I can just add that using a standard WCF DataContract serialization everything works fine – ADE May 31 '12 at 13:56
  • the problem is: it is hard for me to try to guess without being able to see the code and/or repro the scenario :( – Marc Gravell May 31 '12 at 14:16
  • No problem Marc, thanks a lot for your support. Anyway, it is a scenario pretty easy to reproduce: given a database with to tables (one table references the other one by a foreign key), create an EF model, set up a WCF web service which implements this kind of logic var myClassTO = new MyClassTO(); myClassTO.ListOfMyEntities = Context.MyEntity.Include("ReferencedEntity.AnotherReferencedEntity").ToList; return myClassTO; and check whether the client receives the entities, including the referenced one. – ADE May 31 '12 at 14:24
  • again, I strongly suspect the problem here is the back-reference, from the referenced-entity back to MyEntity – Marc Gravell May 31 '12 at 14:50

0 Answers0