You do not need to write BsonClassMap.RegisterClassMap<MyClass>(); , because by default all classes will be automatically deleted.
You should use RegisterClassMap when you need custom serialization:
BsonClassMap.RegisterClassMap<MyClass>(cm => { cm.AutoMap(); cm.SetIdMember(cm.GetMemberMap(c => c.SomeProperty)); });
You can also use attributes to create control serialization (for me this looks more familiar):
[BsonId] // mark property as _id [BsonElement("SomeAnotherName", Order = 1)] //set property name , order [BsonIgnoreExtraElements] // ignore extra elements during deserialization [BsonIgnore] // ignore property on insert
You can also create global rules that are used in autopilot, for example:
var myConventions = new ConventionProfile(); myConventions.SetIdMemberConvention(new NoDefaultPropertyIdConvention()); BsonClassMap.RegisterConventions(myConventions, t => true);
I use only attributes and conventions to control the serialization process.
I hope for this help.
Andrew Orsich
source share