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

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

 class Account { public string Id { get; set; } ... } class AccountStore { static AccountStore() { BsonClassMap.RegisterClassMap(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是:

 // 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 )而不是使用SetRepresentation设置序列化程序。

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

找到答案:

 cm.IdMemberMap .SetRepresentation(BsonType.ObjectId) .SetIdGenerator(StringObjectIdGenerator.Instance); 

这允许我保存为本机ObjectId,并且仍然将C#中表示的Id表示为字符串。 作为一个小问题,必须在查询之前解析id:

 public Account GetAccountById(string id) { return _accounts.FindOneById(ObjectId.Parse(id)); } 

编辑2015年5月
显然,自从我写这个答案后,司机已经改变了。 上面的另一个答案对于较新版本是正确的,但如果使用旧版本的驱动程序,仍然可以参考此答案。

如果您想要在整个实体范围内进行相同类型的映射而无需一遍又一遍地重复自己,则可能需要使用约定:

 public class StringObjectIdIdGeneratorConventionThatWorks : ConventionBase, IPostProcessingConvention { ///  /// Applies a post processing modification to the class map. ///  /// The class map. public void PostProcess(BsonClassMap classMap) { var idMemberMap = classMap.IdMemberMap; if (idMemberMap == null || idMemberMap.IdGenerator != null) return; if (idMemberMap.MemberType == typeof(string)) { idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance).SetSerializer(new StringSerializer(BsonType.ObjectId)); } } } 

…然后使用它代替所有自定义映射:

 ConventionPack cp = new ConventionPack(); cp.Add(new StringObjectIdIdGeneratorConventionThatWorks()); ConventionRegistry.Register("TreatAllStringIdsProperly", cp, _ => true);