2014-10-31 117 views
0

简单的问题,你怎么一个BsonValue的默认值设置为空列表BsonDefaultValue设置为空列表

[BsonElement("Networks")] 
    [BsonRepresentation(BsonType.String)] 
    [BsonIgnoreIfNull] 
    [BsonDefaultValue(new List<SocialProfileTypes>() { })] 
    [BsonIgnoreIfDefault] 
    public UniqueList<SocialProfileTypes> Networks 
    { 
     get { return networks; } 
     set { networks = value; } 
    } 

由于[BsonDefaultValue(new List<SocialProfileTypes>() { })]给出了一个语法错误

Error 8 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type 

回答

0

这是一个C#语言的约束。属性中的值必须是编译时间常量。

至于MongoDB的驱动程序,你需要使用代码来设置这样的值(注:没有编译这一点,但是这将是相似的)

BsonClassMap.RegisterClassMap<MyClass>(cm => 
    cm.AutoMap(); 
    cm.MapMember(x => x.Networks).SetDefaultValue(new List<SocialProfileTypes>()) 
}); 

我相信这是怎么回事无论如何因为List没有实现或从UniqueList继承,所以你也必须解决这个问题。

+0

抱歉,关于唯一列表中的混淆我想将该部分抽象出来,因此我将其替换为列表。唯一列表是列表中的直接后代,因此它应该可以工作。我会尝试一下 – MrX 2014-10-31 12:54:29