Reputation: 13083
It says here (http://mongodb.github.io/mongo-csharp-driver/2.2/reference/bson/mapping/#id-generators) that the driver should use one of the builtin ID generators to generate a new ID for properties ID, id, _id (convention).
I have a model with
public string Id { get;set; }
and the automatic ID generation is not happening. Inserted document has an ID of null.
Snippet from my repository code:
public void Save<TEntity>(TEntity item) where TEntity : class, new()
{
var collection = GetCollection<TEntity>();
collection.InsertOne(item);
}
Upvotes: 1
Views: 1071
Reputation: 13083
After further troubleshooting I got this working.
Had to put attribute [BsonId] set to StringObjectIdGenerator to Id properties.
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
The docs at http://mongodb.github.io/mongo-csharp-driver/2.2/reference/bson/mapping/#id-generators are certainly misleading or not clear enough
Some of these Id generators are used automatically for commonly used Id types: GuidGenerator is used for a Guid ObjectIdGenerator is used for an ObjectId StringObjectIdGenerator is used for a string represented externally as ObjectId
I've opened an issue with MongoDb JIRA tracker and I'll post their reply here if they provide any insight into that.
Upvotes: 1