I'm upgrading from using BsonDocument everywhere, to using deserialization on POCO objects.
Overall the objects are populated with correct values, but i'm having a problem with the code below, values on the EventReferenceEntry.Id property - which are always 0.
Perhaps worth knowing, is that if i remove "BsonIgnoreExtraElements" attribute from the EventReferenceEntry class, i get an error "Element 'i' does not match any field or property of class "
I've also tried setting EventReferenceEntry.Id to Int64 and UInt64, but no difference.
The driver is version 2.7.3, i tried it with a fresh installation of the latest version, but it's the same problem.
The database is a series of events. An event has: _id = Int64 _t = Int32 (the type of the event) _r = an array of objects (references to other objects, entities or events, that are relevant.)
C# Code of the POCO objects
[BsonIgnoreExtraElements]
public class EventEntry
{
[BsonElement("_id")]
public ulong Id { get; set; }
[BsonElement("_t")]
public int Type { get; set; }
public DateTime Time { get { return new DateTime((long)Id, DateTimeKind.Utc); } }
[BsonElement("_r")]
public List<EventReferenceEntry> References { get; set; }
}
[BsonIgnoreExtraElements]
public class EventReferenceEntry
{
[BsonElement("i")]
public UInt64 Id { get; set; }
[BsonElement("n")]
public string Name { get; set; }
[BsonElement("a")]
public int Asset { get; set; }
public EventReferenceEntry()
{
}
}
An example database entry
{
"_id" : NumberLong(637684658186492532),
"_t" : 1058,
"_r" : [
{
"n" : "p",
"i" : NumberLong(637662370697662760)
},
{
"n" : "a",
"a" : 1202
},
{
"n" : "o",
"i" : NumberLong(637684655676255124),
"a" : 2934
}
]
}
Found the problem and solution.
A property in the class (in my case EventReferenceEntry) cannot be named “Id” or “id”, I guess that a property named that is assumed to be bound to the _id bson property. It can however be named “ID” or “Ident” or anything else, and the value gets assigned.