2

通常当我将一个对象存储到我的集合中并向我的对象添加一个新属性时,当在 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 不存在。

有谁知道我如何使反序列化器也以同样的方式反序列化嵌套类?

4

1 回答 1

0

如果您传递特定类型以查找一个,c# 驱动程序会自动反序列化嵌套类,请使用 mongo db 测试以下示例

 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
                }
        ]
}
于 2013-02-01T09:43:49.273 回答