简体   繁体   English

使用StringObjectIdGenerator映射的ID在数据库中生成字符串_id

[英]Mapped Id using StringObjectIdGenerator produces string _id in database

I'm moving over my code to the new 2.0 driver, and having an issue with ObjectIds. 我将代码移至新的2.0驱动程序,并遇到ObjectIds问题。

Previously I had decorated string Id properties with BsonId and BsonRepresentation attributes. 以前,我已经使用BsonId和BsonRepresentation属性装饰了字符串Id属性。

Now i'm using class maps 现在我正在使用类映射

BsonClassMap.RegisterClassMap<Model>(cm =>
       {
           cm.MapIdMember(p => p.Id).SetIdGenerator(StringObjectIdGenerator.Instance);
           cm.SetIgnoreExtraElements(true);
           cm.AutoMap();

       });

model is really simple in this example 这个例子中的模型真的很简单

public class Model
{
   public string Id { get; set; }
   public DateTime UpdatedTs { get; set; }
} 

however after inserting, I get the object id back as string, but it's also a string on the server. 但是,插入后,我将对象ID作为字符串返回,但它也是服务器上的字符串。

Image of MongoVue objectid is string MongoVue objectid的图像是字符串

Is there something I'm missing? 有什么我想念的吗?

Thanks Sam 谢谢山姆

Ok, thanks to some feedback from Craig on the mongodb driver team, the solution I got working was: 好的,多亏了Craig对mongodb驱动程序团队的一些反馈,我得到的解决方案是:

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

Like when decorating the properties of the Id property, you need to set the Representation which in v 2.0 you do by SetSerializer 就像装饰Id属性的属性时一样,您需要设置SetSerializer在v 2.0中执行的Representation

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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