Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将嵌套文档反序列化为特定的类对象?_C#_Mongodb_Mongodb .net Driver - Fatal编程技术网

C# 如何将嵌套文档反序列化为特定的类对象?

C# 如何将嵌套文档反序列化为特定的类对象?,c#,mongodb,mongodb-.net-driver,C#,Mongodb,Mongodb .net Driver,我是MongoDb新手,正在尝试将嵌套在父文档中的文档数组序列化到特定的类对象中 我的文档对象如下所示: Project { "_id" : ObjectId("589da20997b2ae4608c99173"), // ... additional fields "Products" : [ { "_id" : ObjectId("56971340f3a4990d2c199853"), "Produc

我是MongoDb新手,正在尝试将嵌套在父文档中的文档数组序列化到特定的类对象中

我的文档对象如下所示:

Project
{
    "_id" : ObjectId("589da20997b2ae4608c99173"),
    // ... additional fields
    "Products" : [ 
        { 
            "_id" : ObjectId("56971340f3a4990d2c199853"),
            "ProductId" : null,
            "Name" : null,
            "Description" : null,
            // ... quite a few more properties
            "Properties" : null 
        }
    ],
    "CreatorUserId" : LUUID("af955555-5555-5555-5555-f1fc4bb7cfae")
}
public class Project : BaseClasses {
    public ObjectId ParentCreatorUserIdProject { get; set; }  
    // ... additional Fields 
    public IEnumerable<IProjectItem> ProjectItems { get; set; } 
    //public IEnumerable<IProductDetails> Products { get; set; }
}

[DataContract][Serializable]
[BsonIgnoreExtraElements][MongoCollection("products")]
public class Product {
    public string Name { get; set; } 
    public string Description { get; set; 
    // .. a bunch of Fields
} 
因此,基本上它是一个名为
Project
的文档,其中包含
产品
文档的嵌套数组

以下是当前类的外观:

Project
{
    "_id" : ObjectId("589da20997b2ae4608c99173"),
    // ... additional fields
    "Products" : [ 
        { 
            "_id" : ObjectId("56971340f3a4990d2c199853"),
            "ProductId" : null,
            "Name" : null,
            "Description" : null,
            // ... quite a few more properties
            "Properties" : null 
        }
    ],
    "CreatorUserId" : LUUID("af955555-5555-5555-5555-f1fc4bb7cfae")
}
public class Project : BaseClasses {
    public ObjectId ParentCreatorUserIdProject { get; set; }  
    // ... additional Fields 
    public IEnumerable<IProjectItem> ProjectItems { get; set; } 
    //public IEnumerable<IProductDetails> Products { get; set; }
}

[DataContract][Serializable]
[BsonIgnoreExtraElements][MongoCollection("products")]
public class Product {
    public string Name { get; set; } 
    public string Description { get; set; 
    // .. a bunch of Fields
} 
我的尝试 我试图使用
BsonClassMap
创建一个映射,但是,尽管阅读了。我应该注意到,我已经和以前的开发人员一起定义了这个类映射和其他映射,并且确信(单元测试)它们是可以执行的

  BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
      cm.AutoMap();
      cm.SetDiscriminator("ProjectItem");
      cm.SetDiscriminatorIsRequired(true);
  });
BsonClassMap.RegisterClassMap(cm=>{
cm.AutoMap();
设置鉴别器(“投影项”);
cm.设置所需的鉴别器(真);
});
不幸的是,文档仍然被序列化到
产品的数组中

我的问题
如何将这个嵌套的产品数组序列化为ProjectItem类数组?

因此答案非常简单。。。无论您存储它的类型是什么,都是您想要设置为鉴别器的类型

  BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
      cm.AutoMap();
      cm.SetDiscriminator("Product"); // <--- it was stored as a Product
      cm.SetDiscriminatorIsRequired(true);
  });
由于我们的产品是以“ProductDetail”的鉴别器类型存储的,因此我们将其序列化为如下项目项:

"Products" : [ 
    { 
         "_t" : "ProductDetails",
         "_id" : ObjectId("56971340f3a4990d2c199853"),
         "ProductId" : null,
         "Name" : null,
         "Description" : null,
         // ... quite a few more properties
         "Properties" : null 
    }
]
  BsonClassMap.RegisterClassMap<ProjectItem>(cm => {
      cm.AutoMap();
      cm.SetDiscriminator("ProductDetails"); // <--- it was stored as a ProductDetails
      cm.SetDiscriminatorIsRequired(true);
  });
BsonClassMap.RegisterClassMap(cm=>{
cm.AutoMap();

cm.SetDiscriminator(“ProductDetails”);//如果您需要查看更多示例,请告诉我。起初,这让我有点困惑,在我看到足够多的示例后,它只是单击了一下