By default, Entity Framework will be automatically creating a join table when he sees a many to many relationship. For example let’s take the following example. Let’s say I have two entities named Person and Hobby. Person can have many hobbies. Single Hobby can be used by many persons. If I model that scenario, this is how it will look like.So the above code will create me the following table structure.
Now let’s say, since a person can have many hobbies, I need to track the primary hobby of a person. So for that I will need to modify the join table (PersonHobbies) to add a new column to state whether that particular hobby is the primary hobby. But since Entity Framework is automatically creating the join table, initially it seems there is no way that I can modify the join table.
But there is. Let’s create the join table manually. For that I am creating a new class named “PersonHobbies”.
Next what is left to be done is adding primary key and foreign keys in the PersonHobbies entity. For that I am using the Fluent API in the overriding OnModelCreating event in my DbContext.
So now the above will create me the following table structure as expected.