MongoDB C#驱动程序-如何将_id存储为ObjectId但映射到字符串Id属性?

内森·里德利(Nathan Ridley)

我很难让我的模型将实体的Id属性表示为字符串,但无法自动生成并由MongoDb内部将其表示为native ObjectId

class Account
{
    public string Id { get; set; }
    ...
}

class AccountStore
{
    static AccountStore()
    {
        BsonClassMap.RegisterClassMap<Account>(cm =>
        {
            cm.AutoMap();
            cm.SetIgnoreExtraElements(true);

            // map Id property here
        });
    }

    public void Save(Account account)
    {
        _accounts.Save(account);
    }
}

对于// map Id property here上面的代码行,我尝试了多种不同的方法来配置Id映射,但没有一种有效。我尝试过的方法以及调用该Save方法时引发的相关异常是:

// Exception: No IdGenerator found.
cm.IdMemberMap
  .SetRepresentation(BsonType.ObjectId);

// Exception: No IdGenerator found.
cm.IdMemberMap
  .SetRepresentation(BsonType.String);

// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
  .SetRepresentation(BsonType.ObjectId)
  .SetIdGenerator(ObjectIdGenerator.Instance);

// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
  .SetRepresentation(BsonType.String)
  .SetIdGenerator(ObjectIdGenerator.Instance);

// Exception: Unable to cast object of type 'MongoDB.Bson.ObjectId' to type 'System.String'.
cm.IdMemberMap
  .SetIdGenerator(ObjectIdGenerator.Instance);

我究竟做错了什么?我以为这是ID处理的标准用例?

苯铬

这已经改变了,我使用的是最新的1.x驱动程序(Nuget软件包<package id="mongocsharpdriver" version="2.0.0" targetFramework="net45" />),而不是使用SetRepresentation您设置序列化程序。

public class RegistrationAttempt
{
    public string AttemptId { get; set; }
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用官方的MongoDB C#驱动程序将BsonDocument转换为强类型对象?

来自分类Dev

如何使用C#驱动程序检查MongoDB中是否存在集合?

来自分类Dev

将SetFields与MongoDB C#驱动程序2.0结合使用

来自分类Dev

MongoDB C#驱动程序-如何查询子文档数组上的属性

来自分类Dev

MongoDB C#驱动程序:用于在副本集上进行分片的连接字符串

来自分类Dev

mongodb:如何使用C#驱动程序创建文本索引?

来自分类Dev

使用MongoDB C#驱动程序在sureIndex之后,Id字段映射被破坏

来自分类Dev

如何使用C#驱动程序在mongodb中使用$ currentdate

来自分类Dev

如何在C#驱动程序中编写以下MongoDB查询?

来自分类Dev

MongoDB C#驱动程序-如何将_id存储为ObjectId但映射到字符串Id属性?

来自分类Dev

MongoDB C#驱动程序和服务器生成的ObjectId

来自分类Dev

如何使用C#驱动程序以字符串格式查询MongoDB?

来自分类Dev

如何使用C#驱动程序删除mongodb文档中的嵌套数组元素

来自分类Dev

mongodb c#驱动程序-继承,映射和序列化问题

来自分类Dev

如何在不指定类的情况下使用MongoDB C#驱动程序

来自分类Dev

如何使用MongoDB C#驱动程序进行$ lookup?

来自分类Dev

MongoDB C#驱动程序2.2.2 IBsonSerializer上的'ValueType'属性的用途是什么

来自分类Dev

MongoDb 2.0 C#驱动程序未在插入时为ID创建值

来自分类Dev

使用MongoDB C#驱动程序进行存储桶

来自分类Dev

如何在MongoDB C#驱动程序中按后代属性筛选?

来自分类Dev

如何使用NearSphere MongoDB驱动程序C#将距离结果返回到点

来自分类Dev

Mongodb C#驱动程序Perform字符串包含对嵌入式文档中的属性的查询

来自分类Dev

如何使用MongoDB C#驱动程序聚合$ lookup?

来自分类Dev

MongoDB C#驱动程序和服务器生成的ObjectId

来自分类Dev

如何使用C#驱动程序以字符串格式查询MongoDB?

来自分类Dev

MongoDb C#驱动程序2.0将项目添加到嵌套数组

来自分类Dev

MongoDb 2.0 C#驱动程序未在插入时为ID创建值

来自分类Dev

如何在字符串字段和 ObjectId 之间进行查找 - MongoDB C# 驱动程序

来自分类Dev

我如何将这个 mongodb 查询转换为 c# 驱动程序?

Related 相关文章

  1. 1

    如何使用官方的MongoDB C#驱动程序将BsonDocument转换为强类型对象?

  2. 2

    如何使用C#驱动程序检查MongoDB中是否存在集合?

  3. 3

    将SetFields与MongoDB C#驱动程序2.0结合使用

  4. 4

    MongoDB C#驱动程序-如何查询子文档数组上的属性

  5. 5

    MongoDB C#驱动程序:用于在副本集上进行分片的连接字符串

  6. 6

    mongodb:如何使用C#驱动程序创建文本索引?

  7. 7

    使用MongoDB C#驱动程序在sureIndex之后,Id字段映射被破坏

  8. 8

    如何使用C#驱动程序在mongodb中使用$ currentdate

  9. 9

    如何在C#驱动程序中编写以下MongoDB查询?

  10. 10

    MongoDB C#驱动程序-如何将_id存储为ObjectId但映射到字符串Id属性?

  11. 11

    MongoDB C#驱动程序和服务器生成的ObjectId

  12. 12

    如何使用C#驱动程序以字符串格式查询MongoDB?

  13. 13

    如何使用C#驱动程序删除mongodb文档中的嵌套数组元素

  14. 14

    mongodb c#驱动程序-继承,映射和序列化问题

  15. 15

    如何在不指定类的情况下使用MongoDB C#驱动程序

  16. 16

    如何使用MongoDB C#驱动程序进行$ lookup?

  17. 17

    MongoDB C#驱动程序2.2.2 IBsonSerializer上的'ValueType'属性的用途是什么

  18. 18

    MongoDb 2.0 C#驱动程序未在插入时为ID创建值

  19. 19

    使用MongoDB C#驱动程序进行存储桶

  20. 20

    如何在MongoDB C#驱动程序中按后代属性筛选?

  21. 21

    如何使用NearSphere MongoDB驱动程序C#将距离结果返回到点

  22. 22

    Mongodb C#驱动程序Perform字符串包含对嵌入式文档中的属性的查询

  23. 23

    如何使用MongoDB C#驱动程序聚合$ lookup?

  24. 24

    MongoDB C#驱动程序和服务器生成的ObjectId

  25. 25

    如何使用C#驱动程序以字符串格式查询MongoDB?

  26. 26

    MongoDb C#驱动程序2.0将项目添加到嵌套数组

  27. 27

    MongoDb 2.0 C#驱动程序未在插入时为ID创建值

  28. 28

    如何在字符串字段和 ObjectId 之间进行查找 - MongoDB C# 驱动程序

  29. 29

    我如何将这个 mongodb 查询转换为 c# 驱动程序?

热门标签

归档