EF Code First Many to Many mapping using an existing mapping class

I have three classes

public class User { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<User> Users { get; set; } } public class ProductXUser // Mapping class { public int Id { get; set; } public int User_Id { get; set; } public int Product_Id { get; set; } public DateTime DateMapped { get; set; } } 

How can I map many or many relationships (using the Fluent API) between the User and Product classes using the ProductXUser class as a mapping table?

+4
source share
1 answer

You can not. When you expose a connection table as an object, you cannot use the many-to-many relationship. Instead, you should use two one-to-many relationships. One from User to ProductXUser and one from Product to ProductXUser . You must also change the navigation properties in both Product and User to point to the ProductXUser collection. The direct many-to-many relationship only works when you are not expanding the connection table as an entity.

+3
source