EdB EdB - 11 months ago 176
JSON Question

C# MongoDB deserialization of id property

I have a class that looks like this:

public class Object
public ObjectId Id { get; set; }
public List<Record> Records { get; set; }

public class Record
public ObjectId Id { get; set; }
public Dictionary<string, object> Values { get; set; }

I'm trying to deserialize the following json:

"Id": "5791fad6fcef4434d0318d69",
"Values": {
"Name": "some name....",
"Account": {
"Value": "123",
"Name": "Test Account"

With the mongodb c# driver:

var record = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<Record>(BsonDocument.Parse(value.ToString()));

But I keep receiving the error: Element 'Id' does not match any field or property of class Record.

Any ideas as to why it won't recognize the id property?


It turns out that if you change the property name to the following then the object is deserialized correctly:

public ObjectId _id { get; set; }