Do we need a property of Customer together with a property of CustomerId in Order model class?
I am very new to Entity Framework and data-based application.
Let the Customer
data model class be as follows:
public class Customer
{
public int CustomerId {get;set;}
public string Name {get;set;}
//others properties have been omitted for the sake of simplicity.
}
And the Order
data model:
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 simplic开发者_运维百科ity.
}
My question is: "Do we need a property of Customer
together with a property of CustomerId
in Order
model class?"
No, you don't. The Customer
object in Order
class is sufficient to identify the customer ID. Further, you might want a collection of orders in Customer
class, so that you know how many orders the customer has easily, something like this:-
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
}
So, if you want to retrieve the customer ID from an order, you will do something like this:-
order.getCustomer().getCustomerId();
精彩评论