2011-02-07 14 views
0

我很新,實體框架和基於數據的應用程序。我們是否需要客戶的屬性以及訂單模型類中的CustomerId屬性?

Customer數據模型類如下:

public class Customer 
{ 
    public int CustomerId {get;set;} 
    public string Name {get;set;} 
    //others properties have been omitted for the sake of simplicity. 
} 

而且Order數據模型:

public class Order 
{ 
    public int OrderId {get;set;} 
    public int CustomerId {get;set;} 
    public Customer Customer {get;set;} 
    // other properties have been omitted for the sake of simplicity. 
} 

我的問題是:「我們是否需要的Customer屬性與屬性一起CustomerId in Order model class?「

回答

2

不,你不知道。 Order類中的Customer對象足以識別客戶ID。此外,您可能希望訂單的集合Customer類,讓你知道客戶有多少訂單很容易,像這樣: -

public class Customer { 
    private Long customerId; 
    private String name; 
    private Set<Order> orders = new HashSet<Order>(); 

    // ... getters/setters 
} 

public class Order { 
    private Long orderId; 
    private Customer customer; 

    // ... getters/setters 
} 

所以,如果你想從一個順序檢索客戶ID ,你會做這樣的事情: -

order.getCustomer().getCustomerId(); 
+0

考慮到性能,哪一個更好:具有在`Order`類`Customer`屬性或具有'Order`類`CustomerId`財產? – xport 2011-02-07 04:25:51

相關問題