存储对父文档的引用而不是存储文档的副本
问题描述:
在 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
}
请参阅文档了解更多信息.