I am migrating from RavenDB to MongoDB and therefore want to reuse existing classes deriving from
abstract class Entity
{
protected Entity()
{
}
protected Entity(string id)
{
this.Id = id;
}
public string Id { get; private set; }
}
so far I register like this:
BsonClassMap.RegisterClassMap<Entity>(
x =>
{
x.MapIdField(b => b.Id);
x.SetIgnoreExtraElements(true);
}).AutoMap();
This works fine for inserting documents with already existing Id but fails for inserting documents that come without it. In this case I want MongoDb to create an Id for me. How would I do this without breaking anything else?
Edit: Maybe I need to point out what I mean by "fails". I mean that no Id is beeing generated by MongoDb. Id would always be null if not set by myself.