Hope someone can help. I am going to deal with the C # driver for MongoDB and how it handles serialization. Consider the following class examples:
class Thing { [BsonId] public Guid Thing_ID { get; set; } public string ThingName {get; set; } public SubThing ST { get; set; } public Thing() { Thing_ID = Guid.NewGuid(); } } class SubThing { [BsonId] public Guid SubThing_ID { get; set; } public string SubThingName { get; set; } [BsonIgnore] public Thing ParentThing { get; set; } public SubThing() { SubThing_ID = Guid.NewGuid(); } }
Note that SubThing has a property that refers to the parent. Therefore, when creating objects, I do like this:
Thing T = new Thing(); T.ThingName = "My thing"; SubThing ST = new SubThing(); ST.SubThingName = "My Subthing"; T.ST = ST; ST.ParentThing = T;
The ParentThing property is set to BsonIgnore, because otherwise it will cause a circular reference when serialized in MongoDB.
When I do serialization in MongoDB, it creates the document exactly as I expect:
{ "_id" : LUUID("9d78bc5c-2abd-cb47-9478-012f9234e083"), "ThingName" : "My thing", "ST" : { "_id" : LUUID("656f17ce-c066-854d-82fd-0b7249c80ef0"), "SubThingName" : "My Subthing" }
Here's the problem: when I deserialize, I lose the SubThing link to her parent. Is there a way to set up deserialization so that the ParentThing property is always its parent document?
source share