2013-03-04 54 views
3

在訂單和訂單行的經典案例中,每個訂單行都有對其父訂單的引用。當我將OrderLine保存到RavenDB時,它將整個Order記錄的副本保存爲每個OrderLine的屬性。我如何才能將它僅保存到訂單的鏈接?就像這樣:存儲對父文檔的引用,而不是存儲文檔的副本

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

,而不是這個

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

回答

3

歡迎非關係數據庫!訂單行不會獲得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

+0

確實馬特,但即使在您提到的鏈接中,OrderLine部分也會存儲對其產品的引用,而不是存儲該產品所有屬性的副本。當我的OrderLine類具有產品類型的產品屬性時,我該如何實現?我是否必須將我的OrderLine類更改爲具有類型爲string的ProductID屬性? – 2013-03-05 02:24:56

+0

考慮您希望訂單行中的產品信息與訂單時的完全相同。如果產品更改或被刪除,則不應影響過去的訂單或訂單項次。因此,您應該將所需產品的字段複製到訂單行中。您可能需要一個'ProductId'作爲參考,但您應該將產品說明和價格單獨歸一化到訂單行中。你可以爲此創建一個容器類(例如'ProductRef'),或者你可以單獨存儲這些字段。 – 2013-03-05 02:44:29

+0

另外,閱讀[this](http://ravendb.net/docs/client-api/querying/handling-document-relationships) – 2013-03-05 02:45:46