Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# Driver Serialization with dynamic objects

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:

enter image description here

anyone had any experience with dynamics and mongo?

like image 896
amcdnl Avatar asked Feb 11 '14 21:02

amcdnl


1 Answers

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.

like image 94
i3arnon Avatar answered Sep 19 '22 08:09

i3arnon