I\'m currently applying the [BsonRepresentation(BsonType.String)]
attribute to all Guid
properties in my domain models to have those properties ser
While using conventions will work, pay attention to two important (and related) points:
filter
parameter is required, and if the filter is too general (for example: t => true
), it can overwrite other registered conventions. Another option is to create a BSON Class Map for type Guid, which sets the representation to string:
if (!BsonClassMap.IsClassMapRegistered(typeof(Guid))) {
BsonClassMap.RegisterClassMap(cm => {
cm.AutoMap();
cm.Conventions.SetSerializationOptionsConvention(new TypeRepresentationSerializationOptionsConvention(typeof(Guid), BsonType.String));
});
}
This should be done before any reading/writing using BsonSerializer, otherwise the default Class Map will be created, and you wont be able to change the Class Map.