Entity framework 在订单模型类中,我们是否需要Customer属性和CustomerId属性?
我对实体框架和基于数据的应用程序非常陌生 让Entity framework 在订单模型类中,我们是否需要Customer属性和CustomerId属性?,entity-framework,database-design,Entity Framework,Database Design,我对实体框架和基于数据的应用程序非常陌生 让Customer数据模型类如下所示: public class Customer { public int CustomerId {get;set;} public string Name {get;set;} //others properties have been omitted for the sake of simplicity. } 以及订单数据模型: public class Order { public
Customer
数据模型类如下所示:
public class Customer
{
public int CustomerId {get;set;}
public string Name {get;set;}
//others properties have been omitted for the sake of simplicity.
}
以及订单
数据模型:
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.
}
我的问题是:“在
订单
模型类中,我们是否需要客户
属性和客户ID
属性?”。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
}
order.getCustomer().getCustomerId();
考虑到性能,哪一个更好:在
Order
类中有Customer
属性,还是在Order
类中有CustomerId
属性?它们都差不多,性能差异可以忽略不计。此外,您希望将所有与客户相关的信息分组到customer
类中,并且通过在Order
类中使用customerId
属性,您就失去了数据封装的意义。我的意思是,如果您在两个位置拥有customerId
属性,并且有一天customerId
值发生变化,那么您需要在所有位置更改customerId
值。谢谢+谢谢你的解释。