Here is an example of how to model a many-to-many relation with Entity Framework using database first.

UseCase: one user can have multiple roles and one role can have muliple users. This results in a many-to-many relationship between user and role.

What I like to model looks like this:

many-to-many relation between user and role

many-to-many relation between user and role

We need to model three tables: User, Role and UserRoles.

User.sql

CREATE TABLE [dbo].[User] (
    [Id]   INT            IDENTITY (1, 1) NOT NULL,
    [Name] NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_dbo.User] PRIMARY KEY CLUSTERED ([Id] ASC)
);

Role.sql

CREATE TABLE [dbo].[Role] (
    [Id]   INT            IDENTITY (1, 1) NOT NULL,
    [Name] NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_dbo.Role] PRIMARY KEY CLUSTERED ([Id] ASC)
);

UserRoles.sql

CREATE TABLE [dbo].[UserRoles] (
    [UserId] INT NOT NULL,
    [RoleId] INT NOT NULL,
    CONSTRAINT [PK_dbo.UserRoles] PRIMARY KEY CLUSTERED ([UserId] ASC, [RoleId] ASC),
    CONSTRAINT [FK_dbo.UserRoles_dbo.Role_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[Role] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo.UserRoles_dbo.User_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE
);

GO
CREATE NONCLUSTERED INDEX [IX_UserId]
    ON [dbo].[UserRoles]([UserId] ASC);

GO
CREATE NONCLUSTERED INDEX [IX_RoleId]
    ON [dbo].[UserRoles]([RoleId] ASC);

The entities belonging to this model look like this:

User.cs

public partial class User
{
    public User()
    {
        this.Roles = new HashSet<Role>();
    }
    
    public int Id { get; set; }
    public string Name { get; set; }
    
    public virtual ICollection<Role> Roles { get; set; }
}

Roles.cs

public partial class Role
{
    public Role()
    {
        this.Users = new HashSet<User>();
    }
    
    public int Id { get; set; }
    public string Name { get; set; }
    
    public virtual ICollection<User> Users { get; set; }
}

Note: Entity Framework does not need to have the explicit correlation table in code!

Further Reading: