元素"Id"与类的任何字段或属性都不匹配 [英] Element 'Id' does not match any field or property of class
本文介绍了元素"Id"与类的任何字段或属性都不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从MongoDB中的集合中得到了结果,结构与下面相同
I got the result from the collection in MongoDB, the structure is the same as below
[DataContract]
public class Father
{
[BsonId]
[DataMember]
public MongoDB.Bson.ObjectId _id { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public List<Child> childs { get; set; }
}
[DataContract]
public class Child
{
[DataMember]
public string Id { get; set; }
[DataMember]
public int Name { get; set; }
}
当我尝试此操作时:
List<Father> f = result.ToList();
它调用Element 'Id' does not match any field or property of the class Model.Child
我认为它只是将"Id"作为其他名称.
I think it just takes 'Id' as something else.
我该如何处理?谢谢
推荐答案
我通过在类声明的顶部添加 [BsonIgnoreExtraElements] 解决了类似的问题. ObjectId由MongoDB内部维护,在App级别不需要,因此在类声明中我不需要此字段.希望这对某人有帮助
I resolved similar issue by adding [BsonIgnoreExtraElements] on top of the class declaration. ObjectId is internally maintained by MongoDB which is not needed at the App level, hence in the class declaration I don't require this field. Hope this would help someone
这篇关于元素"Id"与类的任何字段或属性都不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文