Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

mongodb

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!

like image 834
Grant Avatar asked Feb 02 '15 21:02

Grant


2 Answers

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.

like image 150
Grant Avatar answered Nov 06 '22 03:11

Grant


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"),
like image 44
Soren Avatar answered Nov 06 '22 02:11

Soren