MongoDB C#驱动程序使用动态对象进行序列化 [英] MongoDB C# Driver Serialization with dynamic objects
问题描述
我有一个模型,如下所示:
public class Record
{
public Record()
{
Created = DateTime.Now;
}
public string Id {get;组; }
public string ApplicationId {get;组; }
public Dictionary< string,dynamic>价值{get;组; }
public DateTime Created {get;组;使用MongoDB C#驱动程序存储在MongoDB中的
当我做这样的事情时,这样做很棒:
{
applicationId:52f52db8008a280f583c9ff6,
values: {
52f9410a324e68055f39f8c0:245234
52f941096d82a531e700e56b:sdfasdf
52fa4a96e0b2e0d4e61d0a03:52fa4a9b91c477e367ab99e6
}
}
但是当我尝试添加一个字符串数组,如:
{
applicationId:52f52db8008a280f583c9ff6,
values:{
52f9410a324e68055f39f8c0:245234
52f941096d82a531e700e56b:sdfasdf
52fa4a96e0b2e0d4e61d0a03:52fa4a9b91c477e367ab99e6
52fa85ea16f04a6151e4ea51:[52fa85f2d2ffa4cbdcf538e8,52fa85f205505f671f3d0d7b]
}
}
当我尝试在文档上执行GET时,会给我以下错误:
MongoDB.Driver.dll中发生类型为System.IO.FileFormatException的异常,但是n在用户代码中处理
附加信息:反序列化类API.Models.Record.Record的Values属性时出错:无法创建抽象类。
如果我看看它保存的数据库,但它真的很漂亮:
$ b $任何人都有任何关于动态和mongo的经验?
更新: 动态
现在由v2.0驱动程序支持。
使用动态
和 MongoDB
的C#驱动程序。以下是关于它的 jira ticket 。
从描述中
我们还没有确定在哪个版本中我们可能会添加对C#动态类型的支持。这张票是您可以评论该功能的可取性和/或投票的地方。
I've got a model that looks like:
public class Record
{
public Record()
{
Created = DateTime.Now;
}
public string Id { get; set; }
public string ApplicationId { get; set; }
public Dictionary<string, dynamic> Values { get; set; }
public DateTime Created { get; set; }
}
that's stored in MongoDB using the MongoDB C# Driver. This works GREAT when I do things like:
{
applicationId: "52f52db8008a280f583c9ff6",
values: {
"52f9410a324e68055f39f8c0": 245234
"52f941096d82a531e700e56b": "sdfasdf"
"52fa4a96e0b2e0d4e61d0a03": "52fa4a9b91c477e367ab99e6"
}
}
but when I try to add an array of strings like:
{
applicationId: "52f52db8008a280f583c9ff6",
values: {
"52f9410a324e68055f39f8c0": 245234
"52f941096d82a531e700e56b": "sdfasdf"
"52fa4a96e0b2e0d4e61d0a03": "52fa4a9b91c477e367ab99e6"
"52fa85ea16f04a6151e4ea51": [ "52fa85f2d2ffa4cbdcf538e8", "52fa85f205505f671f3d0d7b"]
}
}
It gives me the following error when I try to do a GET on the document:
An exception of type 'System.IO.FileFormatException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: An error occurred while deserializing the Values property of class API.Models.Record.Record: Cannot create an abstract class.
if I look at the database it saved it but its really funky looking:
anyone had any experience with dynamics and mongo?
Update: dynamic
is now supported by the v2.0 driver.
You can't use dynamic
and MongoDB
's C# driver. Here's a the jira ticket about it.
From the description:
We have not yet determined in which version we might add support for C# dynamic types. This ticket is a place where you can comment on the desirability of the feature and/or vote for it.
这篇关于MongoDB C#驱动程序使用动态对象进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!