2012-08-16 129 views
2

我使用MongoDB的官方驱动程序,我想用默认元素的命名设置为小写,以避免这样的代码:MongoDB:默认情况下是否可以设置BsonElement的大小写?

public class Localization 
{ 
    [BsonId(IdGenerator = typeof(ObjectIdGenerator))] 
    public int Id { get; set; } 
    [BsonRequired] 
    [BsonElement("country")] 
    public string Country { get; set; } 

在此示例中,我想,默认情况下该元素的名称是“国家“而不是”国家“又名小写字母。可能吗?

谢谢

回答

9

一个小更新,因为BsonClassMap.RegisterConventions被标记为已过时

var camelCaseConventionPack = new ConventionPack { new CamelCaseElementNameConvention() }; 
ConventionRegistry.Register("CamelCase", camelCaseConventionPack, type => true); 
+0

此外,请确保在使用BsonClassMap注册类型之前添加此约定,否则您的时间就会很糟糕! – Dusda 2015-10-16 00:08:29

3
var conventions = new ConventionProfile(); 
conventions.SetElementNameConvention(new CamelCaseElementNameConvention()); 

BsonClassMap.RegisterConventions(conventions, t => true); 

MongoDB CSharp Driver Serialization Tutorial

+1

虽然我能体会我的答案的unacceptance如果这真的更好,这个问题明确提到小写,而不是骆驼的情况。由于不包括小写惯例,因此您需要编写自己的(因此我的答案)。如果他合理地希望为他的领域提供骆驼案例,那么我们应该编辑该问题以反映未来的围观者。 – 2012-12-09 22:41:21

+1

答案已过时。建议的解决方案使用过时的驱动程序方法。 – 2013-04-20 08:24:11

相关问题