I am using MongoDB to deserialise a document into a C# object and am having issues with ObjectID fields. This is the error I am getting:
Required element ‘LayoutId’ for property ‘LayoutId’ of class MyProject.Data.MyDocument is missing.
Which (if it doesn’t look odd anyways) does make sense because the LayoutId field IS specified:
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace MyProject.Data
{
public class MyDocument
{
public override ObjectId Id { get; set; }
[BsonRequired]
public String Name { get; set; }
[BsonRequired]
public ObjectId LayoutId { get; set; }
}
}
And its populated in the database:

So what other aspects of the serialisation process could prevent this element from being populated? Is there a bug in a particular version of the driver? Am I using the right data type?
It should also be noted that:
- this document was used to populate that value in the database, so it appears that only deserialisation is an issue and serialisation is fine.
- there are other documents in the same library which have the same issue.
- there are mongo documents/contracts in the same solution but in a different library which do NOT seem to have the same prolem,
Thank you Craig for attempting to solve this problem. It turns out that I had not written the question properly.
The problem was that I was calling “SetFields()” on the result, and the fields I was requesting did not include “LayoutId”. I would have expected a more descriptive error from the driver though.