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?
public User() { Orders = new List<Order>(); }
public User() { Orders = new List<Order>(); }
- this initialization string of theOrders
collection is needed not to catchException
when trying to add an instance ofOrder
to the un-initialized collection - Bald