解释空数组[]的嵌入文档值时C#抛出错误

时间:2016-02-18 08:25:07

标签: c# arrays mongodb null embedded-documents

以下是有关我的开发环境的信息:

MongoDB 3.0.0

MongoDB C#驱动程序版本1.7.0.4714

Microsoft Visual Studio Professional 2013

.NET Framework 4.0

这是一个C#类,其对象将用作嵌入文档:

   public class Location
   {
       public double lng { get; set; }
       public double lat { get; set; }
   }

这是一个用于代表一个人家的C#类:

   public class House
   {
       public String houseDescription { get; set; }
       public Location locOfHouse  { get; set; }
   }

我们有一个移动应用程序组件使用的C#API模块将设置 locOfHouse = []

这意味着在名为House的MongoDB Collection中,我们可以得到以下文档:

{
  "_id" : ObjectId("56c455ee26b49c090019b439"),
  "houseDescription" : "Multi-floor house with elevator and staircase",   
  "locOfHouse" : []
}

在我的C#应用​​程序中,我有以下类映射到BSON

     if (false == BsonClassMap.IsClassMapRegistered(typeof(House)))
        {
            BsonClassMap.RegisterClassMap<House>(cm =>
            {
                cm.AutoMap();
                cm.MapProperty<Location>(c => (Location)c.locOfHouse);

            });

        } 

但是,基于Web的C#应用​​程序在检索上述数据时会抛出以下错误:

An error occurred while deserializing the locOfHouse property of class 
House: Expected a nested document representing the serialized form of a Location 
value, but found a value of type Array instead.

如何以这样的方式更改我的C#应用​​程序代码它不会引发上述错误?

1 个答案:

答案 0 :(得分:0)

更改列表

public Location locOfHouse  { get; set; }

public List<Location> locOfHouse  { get; set; }