I am using MongoDB.Driver 2.11.0 and .Net Standard 2.1. To ensure that a database exists and a collection exists, I have the following code:
IMongoClient client = ...; // inject a Mongo client
MongoDatabaseSettings dbSettings = new MongoDatabaseSettings();
IMongoDatabase db = client.GetDatabase("MyDatabase", dbSettings);
MongoCollectionSettings collectionSettings = new MongoCollectionSettings()
{
GuidRepresentation = GuidRepresentation.Standard,
};
IMongoCollection<MyClass> collection = db.GetCollection<MyClass>("MyClasses", collectionSettings);
In earlier versions of MongoDB.Driver, this code would compile without any warnings. In v2.11.0 I am now getting a warning that "MongoCollectionSettings.GuidRepresentation is obsolete: Configure serializers instead" but I have not been able to find any samples illustrating the new way of setting the Guid serialization format. Does anyone know of other ways to set the serializers for a collection?
If you want to define GuidRepresentation for a specific property, you can do it during the registration of the class map, like so:
BsonClassMap.RegisterClassMap<MyClass>(m =>
{
m.AutoMap();
m.MapIdMember(d => d.Id).SetSerializer(new GuidSerializer(GuidRepresentation.Standard));
});
If you want to do it globally:
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With