How to store C # objects with dynamic properties in RavenDB?

I'm having trouble trying to save an object with a dynamic property in RavenDB

The object I'm trying to save represents order. Order contains a list of orders, so imagine the following Order class:

public class Order {
    public int Id { get; set; }
    public List<Orderline> Orderlines { get; set; }
}

And the Orderline class:

public class Orderline {
    public Product Product { get; set; }
    public int Quantity { get; set; }
    public dynamic Attributes { get; set; }
}

The object I'm trying to save (I will show it using JSON);

{
    "Id": 0,
    "Orderlines": [
        {
            "Product": {
                "Id": 0,
                "Name": "Some product"
            },
            "Quantity": 1,
            "Attributes": {
                "color": "Red"
            }
        }
    ]
}

Saving it does not cause errors
RavenDB saves the Order object as

{
    "Id": 0,
    "Orderlines": [
        {
            "Product": {
                "Id": 0,
                "Name": "Some product"
            },
            "Quantity": 1,
            "Attributes": {
                "$type": "Newtonsoft.Json.Linq.JObject, Newtonsoft.Json",
                "color": {
                    "$type": "Newtonsoft.Json.Linq.JValue, Newtonsoft.Json",
                    "$values": []
                }
            }
        }
    ]
}

Please note that the value of the property is Order.Orderlines[0].Attributes.colornot set ...

When I try to convert an object to a C # Order object, I get the following exception:

Cannot start object of type 'Raven.Imports.Newtonsoft.Json.Utilities.CollectionWrapper`1 [Newtonsoft.Json.Linq.JToken] to enter "Newtonsoft.Json.Linq.JValue".

, RavenDB ?

+4
2

'dynamic', -, . ExpandoObject, RavenDB Attributes JSON ( $type $values, )

RavenDB ExpandoObject.

, ExpandoObject (, " " ) "".

+2

, ? , , ExpandoObject

+2

Source: https://habr.com/ru/post/1648262/


All Articles