将mongoDb中的Id属性映射到c#

时间:2016-03-24 22:35:17

标签: c# json mongodb mongodb-.net-driver

我有一个存储在MongoDB中的嵌套文档,例如:

{ 
  _id: 56f19c2663e43f940a7fab69, 
  propA: "some value",
  nestedItem: {
                propB: "nested property",
                leafItem: {
                            id: 1458607372380,
                            leafProp: "a leaf property"
                          }
               }
}

我正在尝试将其映射到一组C#类:

public class FirstLevel {
     public ObjectId _id {get; set;}
     public string propA {get; set;}
     public SecondLevel nestedItem {get; set;}
}

public class SecondLevel {
     public string propB {get; set;}
     public ThirdLevel leafItem {get; set;}
}

[BsonIgnoreExtraElements]
public class ThirdLevel {
     public long id {get; set;}
     public string leafProp {get; set;}
}

如果删除'BsonIgnoreExtraElements',则会出现An error occurred while deserializing the leafItem property of class ConsoleApplication1.SecondLevel: Element 'id' does not match any field or property of class ConsoleApplication1.ThirdLevel.

错误

如果我保留ignore元素,它会在id中填充0。

如果我有[BsonExtraElements]的成员,它会将id显示为属性并显示该值。

为了给出id值的背景知识,我在客户端浏览器应用程序中创建了这个JSON结构,并使用新的Date()。valueOf()来获取id的唯一编号。

当我在MongoVue中查看文档时,我看到该类型被识别为Double,而bson extra元素成员也将其显示为double类型。

我只是想让价值进入班级成员。任何帮助将不胜感激。

由于

0 个答案:

没有答案