通常,当我将对象存储到集合中并向对象添加新属性时,如果在MongoDB中找不到该属性,则mongodb C#驱动程序将使用默认值。但是,当我有一个嵌套类时,官方的MongoDB C#驱动程序似乎无法以相同的方式处理它。查看示例代码:
MongoCollection<Person> peopleCollection = fd.GetCollection<Person>("peopledb");
Person p = peopleCollection.FindOne(); //errors here w/ Type Cast Exception
在将新成员添加到嵌套类时发生:
class Address {
string street;
string zip;
string state; //newly added member, causing deserialization errors/cast exceptions
}
class Person {
ObjectId _id;
string firstname;
string lastname;
Address personAddress;
}
在这种情况下,当我执行FindOneAs时,它将出错,因为Person.personAddress.state不存在。
有人知道我怎样才能使反序列化器也以相同的方式反序列化嵌套类吗?
最佳答案
如果您传递特定类型以找到一种使用mongo db测试下面的示例,则c#驱动程序会自动反序列化嵌套类
public ActionResult Index()
{
// MongoDatabase databse = MongoDatabase.Create("mongodb://localhost:27017/BeniSoftLabs");
MongoDatabase mongodb = DBConn.DBConn.getDBConn();
var menu = mongodb.GetCollection<Menu>("Menu").FindOne();
}
public class Node
{
public virtual string NodeTitle { get; set; }
public virtual string Link { get; set; }
public virtual string IconName { get; set; }
public virtual string ClassName { get; set; }
public virtual IList<Node> NodeList { get; set; }
}
public class Menu
{
public virtual ObjectId id { get; set; }
public virtual IList<Node> MenuList { get; set; }
public virtual IList<Node> RightMenuList { get; set; }
}
数据模型是
{
"_id" : ObjectId("50fecb7d60df893ad0e25138"),
"MenuList" : [
{
"NodeTitle" : "About Us",
"Link" : "/href.html",
"IconName" : "icon-home",
"ClassName" : "active",
"NodeList" : null
},
{
"NodeTitle" : "Our Solution",
"Link" : null,
"IconName" : "icon-briefcase",
"ClassName" : "dropdown",
"NodeList" : [
{
"NodeTitle" : "HTML5 Apps",
"Link" : "#/HTML5Apps",
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "CSS3 Apps",
"Link" : "#/CSS3Apps",
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Single Page Apps",
"Link" : "#/SPAApps",
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Smart Phone Apps",
"Link" : "#/SMartPhoneApps",
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : ".Net Enterprise Apps",
"Link" : "#/NETEnterprise",
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Node.js Enterprise Apps",
"Link" : "#/NodeEnterprise",
"IconName" : null,
"ClassName" : null,
"NodeList" : null
}
]
},
{
"NodeTitle" : "Technologies",
"Link" : null,
"IconName" : "icon-briefcase",
"ClassName" : "dropdown",
"NodeList" : [
{
"NodeTitle" : "HTML5 & CSS3",
"Link" : null,
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Angular.js",
"Link" : null,
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Node.js",
"Link" : null,
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Asp.net MVC",
"Link" : null,
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "MongoDB",
"Link" : null,
"IconName" : null,
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "My Sql",
"Link" : null,
"IconName" : null,
"ClassName" : null,
"NodeList" : null
}
]
},
{
"NodeTitle" : "Why Us",
"Link" : null,
"IconName" : "icon-fire",
"ClassName" : null,
"NodeList" : null
}
],
"RightMenuList" : [
{
"NodeTitle" : "Join Us",
"Link" : null,
"IconName" : "icon-eye-open",
"ClassName" : null,
"NodeList" : null
},
{
"NodeTitle" : "Contact Us",
"Link" : null,
"IconName" : "icon-user",
"ClassName" : null,
"NodeList" : null
}
]
}
关于c# - 使用C#驱动程序反序列化嵌套类时,MongoDB中出现错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14637905/