Reputation: 814
I want to deserialize document which looks like this:
{
"_id" : ObjectId("58e67bd8df79507aa3f8a6b6"),
"value" : 10,
"stack" : [
{
"id" : "49ccbadf-5964-11e6-b1e9-549f3520935c",
"value" : 0
},
{
"id" : "49ccb5cc-5964-11e6-b1e9-549f3520935c",
"value" : 0
}
]
}
to a C# class. The class name is Entry and its definition looks like this:
public class Entry
{
public BsonObjectId _id { get; set; }
public int value { get; set; }
public List<Stack> stack { get; set; }
}
and the Stack class looks like this
public class Stack
{
public string id { get; set; }
public int value { get; set; }
}
When querying I get the following exception
'An error occurred while deserializing the stack property of class ConsoleApp4.Entry: Element 'id' does not match any field or property of class ConsoleApp4.Stack.'
I can decorate the stack class with [BsonIgnoreExtraElements] attribute and then the exception is gone but I don't want to do this since I have to use the id property in querying. Anyone has any idea what I should do? I would appreciate any help.
Upvotes: 1
Views: 1418
Reputation: 66
To use the ObjectId class you need to pull in the nuget package MongoDB.Bson this will allow you to map the _id property
Upvotes: 0
Reputation: 3349
I do not think you need that either. Just change the type like this:
public ObjectId Id { get; set; }
Upvotes: 0
Reputation: 814
Got it by doing through some trial and error - enough to decorate the stack class with [BsonNoId] attribute.
Upvotes: 3