entity-framework – Mapping relationship with Entity Framework Code First: One-to-many and Many-to-many – Many-to-many

Let’s move on to the other scenario, where every person can have multiple cars and every car can have multiple owners (but again, the relationship is bidirectional). This is a many-to-many relationship. The easiest way is to let EF do it’s magic using conventions.

Just change the model like this:

 public class Person
{
   public int PersonId { get; set; }
   public string Name { get; set; }
   public virtual ICollection<Car> Cars { get; set; }
}

public class Car
{
   public int CarId { get; set; }
   public string LicensePlate { get; set; }        
   public virtual ICollection<Person> Owners { get; set; }
}

And the schema:


Almost perfect. As you can see, EF recognized the need for a join table, where you can keep track of person-car pairings.

if you want to reproduce, please indicate the source:
entity-framework – Mapping relationship with Entity Framework Code First: One-to-many and Many-to-many – Many-to-many - CodeDay