MongoDB C#驱动程序使用动态对象进行序列化

时间:2014-02-11 21:23:42

标签: c# .net mongodb dynamic mongodb-.net-driver

我有一个看起来像的模型:

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; }
}

使用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时,它给出了以下错误:

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.

如果我查看它保存的数据库,但它真的很时髦:

enter image description here

任何人都有动力和mongo的经验吗?

1 个答案:

答案 0 :(得分:6)

v2.0驱动程序现在支持

更新: dynamic


您无法使用dynamicMongoDB的C#驱动程序。这是关于它的jira ticket

来自说明:

  

我们尚未确定哪个版本可以添加对C#动态类型的支持。此票证是您可以评论该功能的可取性和/或投票的地方。