Mongo 不通过fulent自动生成ObjectId API

Mongo do not generate ObjectId automatically via fulent API

我正在从 Mongo 驱动程序 1.x 迁移到 2.x 并遇到 Mongo 不简单地生成文档 ID 的问题。

实体:

public sealed class History
{
    public string Id { get; set; }
    public long BusinessId { get; set; }
    public ShowcaseVersion Version { get; set; }
}

映射:

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

插入:

Collection.InsertOne(new History
{
    BusinessId = businessId,
    Version = version
});

结果:

E11000 duplicate key error collection: showcases.History index: id dup key: { : null }

如果您想使用 ObjectId 而不是字符串而不是将其更改为 ObjectId,我认为它应该可行。

但是如果你想像这样使用字符串 post 可能是答案:using string for mongodb _id