存储对父文档的引用而不是存储文档的副本 [英] Store reference to parent document instead of storing copy of document

查看:68
本文介绍了存储对父文档的引用而不是存储文档的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Orders 和 OrderLines 的经典案例中,每个 OrderLine 都有对其父 Order 的引用.当我将 OrderLine 保存到 RavenDB 时,它会保存完整 Order 记录的副本作为每个 OrderLine 的属性.我如何让它只保存订单的链接?像这样:

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

代替这个

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

推荐答案

欢迎使用非关系型数据库!订单行没有 ID.他们不会被放入自己的文件中.它们存储在重要的地方 - 与订单一起!

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

生成如下文件:

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
}

请参阅文档了解更多信息.

这篇关于存储对父文档的引用而不是存储文档的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆