Missing SetRepresentation () method in C # MongoDB.Driver 2.0.0-beta1

I am working with the latest C # driver for MongoDB . Now I know it's beta, but I think I'm doing some basic things.

What is my problem: I'm trying to set the view for my Id field to ObjectId instead of string , as described in the documentation :

 BsonClassMap.RegisterClassMap<Entity>(cm => { cm.AutoMap(); cm.IdMemberMap.SetRepresentation(BsonType.ObjectId); }); 

But I cannot do this because the SetRepresentation() method does not exist. And I can’t find anything like it.

So wondering if this method has been removed? Is there any other way to set a view besides attributes? I cannot use attributes, because I do not have access to the Entity class, I work with a derived class.

Thanks in advance!

+6
source share
2 answers

I spoke with the driver developer, and he clarified this situation:

We have included all these parameters in serializers, so in this case you will need to install a serializer. IdMemberMap.SetSerializer (new StringSerializer (BsonType.ObjectId)); // This is the string that will be represented as the ObjectId in the database.

+14
source

it works for me (v2.2)

 cm.MapIdMember(c => c.Id) .SetSerializer(new StringSerializer(BsonType.ObjectId)) .SetIdGenerator(StringObjectIdGenerator.Instance); 

it represents objectId in the database (not a row)

 "_id" : ObjectId("56715ebddb6986202816e566"), 
+3
source

Source: https://habr.com/ru/post/981894/


All Articles