简体   繁体   中英

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

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

What's my problem: I'm trying to set representation for my Id field to ObjectId instead of string like it is described in documentation :

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

But I can not do that because method SetRepresentation() does not exist. And I can not find anything similar.

So I wonder, was this method removed? Is there any other way to set representation besides attributes? I can not use attributes because I don't have access to Entity class, I'm working with derived class.

Thanks in advance!

I've spoken with the developer of the driver and he clarified this situation:

We've brought all those options into the serializers themselves, so, in this case, you'll want to set the serializer. IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId)); //It's a string which will be represented as an ObjectId in the database.

this works for me (v2.2)

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

it's represent objectId in database (not string)

"_id" : ObjectId("56715ebddb6986202816e566"),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM