Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoCollectionSettings.GuidRepresentation is obsolete, what's the alternative?

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?

like image 604
ElGordo Avatar asked Dec 02 '22 09:12

ElGordo


1 Answers

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));
like image 148
rytisk Avatar answered Dec 23 '22 16:12

rytisk