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 a many to many relationship (using Fluent API) between User class and Product class using ProductXUser class as the mapping table?
You can’t. Once you expose junction table as entity you cannot use many-to-many relation. You must instead use two one-to-many relations. One from
UsertoProductXUserand second fromProducttoProductXUser. You must also change navigation properties in bothProductandUserto point to collection ofProductXUser. Direct many-to-many relation works only when you do not expose junction table as an entity.