我们是否需要在 Order 模型类中同时拥有 Customer 属性和 CustomerId 属性?

发布于 2024-10-15 22:59:28 字数 635 浏览 2 评论 0原文

我对实体框架和基于数据的应用程序非常陌生。

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 的属性在 Order 模型类中具有 CustomerId 属性?”

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 simplicity.
}

My question is: "Do we need a property of Customer together with a property of CustomerId in Order model class?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2024-10-22 22:59:28

不,你不知道。 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文