I want to understand the navigation properties. There is a class:

public class Order { public int Id { get; set; } public int CustomerId { get; set; } public User Customer { get; set; } public int SellerId { get; set; } public User Seller { get; set; } } 

As you can see, here I try to refer to the User class 2 times.

 public class User { public int Id { get; set; } public string Name { get; set; } [InverseProperty("Customer")] public virtual ICollection<Order> MyOrders { get; set; } [InverseProperty("Seller")] public virtual ICollection<Order> OrdersToMe { get; set; } public User() { Orders = new List<Order>(); } } 

Question # 1: Will the Orders navigation property select orders in which the user is Seller or Buyer? How to make a request?

Question # 2: If I want to add a Many-to-Many relationship between these tables (for example, if the Order has several Sellers, but the One-to-Many relationship remains for the Seller), will the addition of the Sellers Navigation property to the Order help? Again the question of their separation.

UPD: Fixed the question in light of the discussion. Why is the Many to Many relationship not created?

  • There are a couple of words about your problem: ru.stackoverflow.com/a/539641/178779 - Pavel Mayorov
  • @PavelMayorov and the second question is solved in the same way? - Sergey Tambovtsells
  • @PavelMayorov and how to be with the string public User () {Orders = new List <Order> (); }? Will she be the same? - Sergey Tambovtsells
  • public User() { Orders = new List<Order>(); } public User() { Orders = new List<Order>(); } - this initialization string of the Orders collection is needed not to catch Exception when trying to add an instance of Order to the un-initialized collection - Bald
  • The navigation property in accordance with a certain logic you have to fill yourself - Bald

0