Store reference to parent document instead of storing copy of document

岁酱吖の 提交于 2019-12-23 20:49:23

问题


In the classic case of Orders and OrderLines, each OrderLine has a reference to its parent Order. When I save my OrderLine to RavenDB, it saves a copy of the full Order record as a property of each OrderLine. How do I get it to save only the link to the Order instead? Like this:

{ 
...other orderline properties...
Order : "orders/123"
}

instead of this

     {
        ...other orderline properties...
        Order : {
    ...all properties for order 123...
        }
}

回答1:


Welcome to non-relational databases! Order lines don't get ids. They don't get put in their own documents. They are stored where it matters - with the Order!

public class OrderLine
{
    public string Product { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

public class Order
{
    public string Id { get; set; }
    public string CustomerId { get; set; }
    public List<OrderLine> Lines { get; set; }
    public decimal Total { get; set; }
}

Produces documents like:

orders/123
{
    "CustomerId": "customers/456",
    "Lines": [
        { "Product": "Foo", "Quantity": 2, "Price": 10.00 },
        { "Product": "Bar", "Quantity": 3, "Price": 20.00 },
        { "Product": "Baz", "Quantity": 4, "Price": 30.00 }
    ]
    "Total": 200.00
}

See the documentation for more.



来源:https://stackoverflow.com/questions/15209310/store-reference-to-parent-document-instead-of-storing-copy-of-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!