Good day. Began to study the Entity Framework. and I write on ASP.net mvc5 an example of an online store. Faced the problem of building a domain model (more precisely, connections between entities). There is an essence Product (Product) and order (Order). It would be easy to establish a many-to-many connection between them. But to avoid duplication of products in the order you need to enter more Quantity (LineProduct). LineProduct is a product and how much it is ordered (for example: Bread - 2, Milk - 10, ...). I have sketched a model, but it seems to me difficult, because I think a typical task is a typical solution. I don't really need the Lines table. I fill in the Products table, the buyer forms Orders. You must be able to select all the products of a single order and which orders include the selected product. Thank you in advance for your participation !!!!!

Products

public class Product { [Key] public int Id { get; set; } [Required(ErrorMessage = "Введите название продукта")] public string Name { get; set; } [Required(ErrorMessage = "Введите цену продукта")] public decimal UnitPrice { get; set; } public string PictureRef { get; set; } public List<Line> Lines { get; set; } = new List<Line>(); } 

Lines

  public class Line { [Key] public int Id { get; set; } [Required] public int Quantity { get; set; } [Required] public Product Product { get; set; } } 

Orders

  public class Order { [Key] public int Id { get; set; } [Required] public Address DeliveryAddress { get; set; } [Required] public virtual List<Line> Lines { get; set; } = new List<Line>(); } 

    1 answer 1

    1. Order details I probably would have put OrderItems in a separate table OrderItems
    2. Since the price of the goods may change over time, so you need to add the price validity period (or maybe even put the cost in a separate table), if the price change history is of no interest, then if you change the cost, you can overwrite the value in the Product table, but then order details added price public decimal Price {get;set;}

     public class Product { public int Id {get;set;} public decimal Price {get;set;} // прочие поля } public class Order { public int Id {get;set;} // прочие поля public virtual ICollection<OrderItems> Items {get;set;} } public class OrderItem { public int Id {get;set;} public int OrderId {get;set;} public int ProductId {get;set;} public decimal Amount {get;set;} public virtual Order Order {get;set;} public virtual Product Product {get;set;} } 
    • Understood thanks. Also in the Product table, you can add "public virtual ICollection <OrderItems> Items {get; set;}" to reach all orders for this product. Or is it just done directly from the OrderItem table? Those. search for all items by ProductId and accordingly all their orders. - Aldmi
    • @Aldmi dall orders for a specific product can be obtained on the basis of the OrderItems tables OrderItems filtering across the ProductId field and the opposite situation when you need to get all the products of the order: filter by OrderId - Bald
    • Those. Is this a full table from which I work through DbSet <OrderItem> OrderItems? - Aldmi