缺少C#MongoDB.Driver 2.0.0-beta1中的SetRepresentation()方法

Gra*_*ant 7 c# mongodb

我正在使用MongoDB的最新C#驱动程序.我知道它现在是测试版,但我认为我正在做一些基本的事情.

我的问题是什么:我正在尝试为我的Id字段设置表示,ObjectId而不是string文档中描述的那样:

BsonClassMap.RegisterClassMap<Entity>(cm =>
{
    cm.AutoMap();
    cm.IdMemberMap.SetRepresentation(BsonType.ObjectId);
});
Run Code Online (Sandbox Code Playgroud)

但我不能这样做,因为方法SetRepresentation()不存在.我找不到类似的东西.

所以我想知道,这种方法被移除了吗?除了属性之外还有其他方法来设置表示吗?我不能使用属性,因为我无法访问Entity类,我正在使用派生类.

提前致谢!

Gra*_*ant 17

我和驱动程序的开发人员谈过,他澄清了这种情况:

我们已经将所有这些选项都带入了序列化器本身,因此,在这种情况下,您需要设置序列化器.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId)); //这是一个字符串,将在数据库中表示为ObjectId.


Sor*_*ren 5

这对我有用(v2.2)

cm.MapIdMember(c => c.Id)
  .SetSerializer(new StringSerializer(BsonType.ObjectId))
  .SetIdGenerator(StringObjectIdGenerator.Instance);
Run Code Online (Sandbox Code Playgroud)

它代表数据库中的 objectId(不是字符串)

"_id" : ObjectId("56715ebddb6986202816e566"),
Run Code Online (Sandbox Code Playgroud)