首页 文章

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

提问于
浏览
7

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

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 回答

  • 6

    v2.0驱动程序现在支持 Update: dynamic .


    你不能使用 dynamicMongoDB 's C# driver. Here'关于它jira ticket .

    从描述:

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

相关问题