将 BsonArray 映射到列表在 POCO [英] Mapping a BsonArray to a List<> in a POCO
问题描述
我使用的是 本教程了解如何将 Mongo 数据库中的文档映射到 POCO 对象.
I was using this tutorial to learn about mapping documents in a Mongo database to POCO objects.
我的文档是这样设置的:
My documents are setup like this:
{
"VehicleEntry" : [
{
"BatteryStatus" : "GOOD",
"VehicleStatus" : "PASSED"
},
{
"BatteryStatus" : "GOOD",
"VehicleStatus" : "PASSED"
}
],
"project_id" : "1234"
}
要注意的主要事情是我在每个文档中都有一个名为 VehicleEntry 的数组.我已经设置了 POCO 来匹配这样的文档:
The main thing to notice about this is that I have an array inside of each document called VehicleEntry. I have setup POCOs to match the documents like this:
public class VehicleEntry : MongoEntity
{
[BsonElement("@Date")]
public String Date { get; set; }
[BsonElement("@Time")]
public String Time { get; set; }
[BsonElement("@Vin")]
public String Vin { get; set; }
[BsonElement("@Year")]
public String Year { get; set; }
[BsonElement("@Body")]
public String Body { get; set; }
[BsonExtraElements]
public BsonDocument ExtraElements { get; set; }
}
public class VehicleDataUpload : MongoEntity
{
public VehicleDataUpload()
{
VehicleEntries = new List<VehicleEntry>();
}
[BsonElement("project_id")]
public String ProjectId { get; set; }
[BsonElement("VehicleEntry")]
public List<VehicleEntry> VehicleEntries { get; set; }
[BsonExtraElements]
public BsonDocument ExtraElements { get; set; }
}
为了测试这一点,我写了一个简单的行来提取使用给定项目标识符找到的第一个文档:
To test this, I wrote one simple line that pulls for the first document found with a given project identifier:
var collection = database.GetCollection<VehicleDataUpload>("vehicleCollection");
var firstProj = collection.AsQueryable().Where(i => i.ProjectId.Equals("myProjIdentifier")).First();
但是,当我运行它时,出现以下异常:
However, when I run it I get the following exception:
附加信息:反序列化类 CARRSMongoTest.VehicleDataUpload 的 VehicleEntries 属性时出错:无法反序列化 BsonType 'Document' 中的 'List'.
Additional information: An error occurred while deserializing the VehicleEntries property of class CARSMongoTest.VehicleDataUpload: Cannot deserialize a 'List' from BsonType 'Document'.
我认为这很简单,因为 VehicleEntry
的类型无法转换为 List<>
对象,但我不知道该怎么做它.当它应该将 VehicleEntry
视为数组类型时,我有点困惑为什么它在错误消息中说类型文档".
I think this is pretty straight forward, as the type of VehicleEntry
cannot be converted to a List<>
object, but I'm not sure how to do it. I'm a little confused why it says type 'Document' in the error message, when it should treat VehicleEntry
as an Array type.
如何将数组映射到 POCO 的属性?
How can I map the array to a property of the POCO?
编辑我已尝试更改 VehicleEntry,使其不从 MongoEntity 扩展,但错误仍然存在.
EDIT I have tried changing VehicleEntry so it doesn't extend from MongoEntity, but the error still persists.
推荐答案
好吧,我意识到这失败的原因是因为我的错误.我们的大多数项目都会有多个车辆条目,但碰巧的是,我正在测试的项目在 XML 中只有一个,因此反序列化将其转换为 Document,而不是 BsonArray.
Well, I realized that the reason this was failing was because of an error on my end. Most of our projects will have multiple vehicle entries, but by chance the one I was testing with only had one in the XML, so the deserialization converted it to a Document, not to a BsonArray.
上面的代码可以工作,假设 VehicleEntry
字段实际上是 BsonArray 类型,驱动程序会将其映射到 List 对象.
The above code will work, assuming the VehicleEntry
field is in fact of type BsonArray, the driver will map it to a List object.
这篇关于将 BsonArray 映射到列表在 POCO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!