简体   繁体   English

使用StringObjectIdGenerator时,MongoDB C#不会为_id / ObjectId生成值

[英]MongoDB C# does not generate a value for _id/ObjectId when using StringObjectIdGenerator

I am trying to follow the example here , but something is not working right. 我正在尝试按照此处的示例进行操作,但是某些方法无法正常工作。 I've implemented the following: 我已经实现了以下情况:

My Model 我的模特

public class MyModel { 
       public string MyModelId { get; set; } 
       //Other properties 
 }

My mapping 我的地图

    BsonClassMap.RegisterClassMap<MyModel>(cm =>
        {
            cm.AutoMap();
            cm.MapIdProperty(c => c.MyModelId)
                    .SetIdGenerator(StringObjectIdGenerator.Instance)
                    .SetSerializer(new StringSerializer(BsonType.ObjectId));

        });

My Insert 我的插入

        if (!BsonClassMap.IsClassMapRegistered(typeof(MyModel)))
        {
            //Do the mapping above
        }

        var collection = RetrieveCollection("MyModels"); //method used to retrieve an IMongoCollection from the database.
        var bsonDoc = model.ToBsonDocument(); //model is an argument passed in to the method of type MyModel

        collection.InsertOne(bsonDoc);

Every time it gets done doing the InsertOne() operation, there is no element in the BsonDocument for MyModelId. 每次完成InsertOne()操作后,BsonDocument中就没有MyModelId元素。 Furthermore, there is an element for _id in the BsonDocument that is always set to "BsonNull". 此外,BsonDocument中有一个用于_id的元素,该元素始终设置为“ BsonNull”。 Then if I try to insert again, MongoDB throws a " E11000 duplicate key error collection:" error, presumably because one already exists with a value of "BsonNull". 然后,如果我尝试再次插入,MongoDB会引发“ E11000重复键错误集合:”错误,大概是因为已经存在一个值“ BsonNull”。

If I comment out my mapping, an ObjectId value gets created for _id, but then I cannot deserialize back to MyModel. 如果我注释掉了映射,则会为_id创建一个ObjectId值,但随后我将无法反序列化回MyModel。

Why doesn't the StringObjectIdGenerator actually generate an ID? 为什么不StringObjectIdGenerator实际生成的ID?

Alright, with the some educated guessing after view This post , I tried the following: 好吧,鉴于查看此帖子后的一些有根据的猜测,我尝试了以下操作:

    BsonClassMap.RegisterClassMap<MyModel>(cm =>
    {
        cm.AutoMap();
        cm.MapIdProperty(c => c.MyModelId)
                .SetIgnoreIfDefault(true) //added this line
                .SetIdGenerator(StringObjectIdGenerator.Instance)
                .SetSerializer(new StringSerializer(BsonType.ObjectId));

    });

This seems to have resolved the issue. 这似乎已经解决了问题。 When inserting the BsonDocument, I still saw the "_id" field, instead of "MyModelId". 插入BsonDocument时,我仍然看到“ _id”字段,而不是“ MyModelId”。 However the "_id" field had a value in it. 但是,“ _ id”字段中有一个值。 Furthermore when I retrieved the value from the collection, and deserialized it back to a MyModel object the MyModelId property was populated by the value previously saw in "_id". 此外,当我从集合中检索值并将其反序列化为MyModel对象时,MyModelId属性由先前在“ _id”中看到的值填充。

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

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