To demonstrate a many to many relationship, we can look at a two-way relationship between Person and Hobby. We start by adding a collection property to each class.
With no mapping file, EF will create a migration that combines the two class’s names and an id column for each.
This migration is identical to what would generated were we to use the most simple mapping available - HasMany(x => x.Hobbies).WithMany(x => x.People)
Optionally, we can add more mapping information to this to ensure the database is updated to match our needs. In this case we want to follow a convention of EntityId and we would like the table name to be HobbyPerson.
As is always the case with string literals, be careful with spelling. The following is completely valid, but probably not something that will make a team member sing your praises.
An additional option in many-to-many scenarios is to model the join table. You may want to store data other than just foreign keys here, or it may just make more sense in your application to work with this table rather than the root elements. Inserting a record here will take care of inserting the related Person or Hobby record, should they not exist. In this case, we are using a composite primary key and must specify this in the mapping file.