在订单和订单行的经典案例中,每个订单行都有对其父订单的引用。当我将 OrderLine 保存到 RavenDB 时,它会将完整订单记录的副本保存为每个 OrderLine 的属性。如何让它只保存订单链接?像这样:
{
...other orderline properties...
Order : "orders/123"
}
而不是这个
{
...other orderline properties...
Order : {
...all properties for order 123...
}
}
最佳答案
欢迎使用非关系数据库!订单行没有 ID。他们不会被放入自己的文档中。它们存储在重要的地方 - 与订单一起!
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
}
请参阅 the documentation 了解更多信息。
关于ravendb - 存储对父文档的引用而不是存储文档的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209310/