'EF Core - Many to many relationship using EntityMappingConfiguration

I'm having a slight problem trying to configure a many-to-many relationship. I have the following classes:

public class User
{
    public int Id;
    public virtual IEnumerable<Group> Groups;
}

public class Group
{
    public int Id;
    public virtual IEnumerable<User> Users;
}

public class UserGroup
{
    public int Id;
    public int UserId;
    public int GroupId;
    public virtual User User;
    public virtual Group Group;
}

I'm trying to set up the relationship like this:

internal class UserGroupMap : EntityMappingConfiguration<UserGroup>
{
    public override void Map(EntityTypeBuilder<UserGroup> b)
    {
        b.ToTable("UsersGroups").HasKey(t => t.Id);

        b.Property(t => t.Id).HasColumnName("Id");

        //Relationships here...
    }
}

But I can't figure out how to set it up. Firstly, are my model classes correctly set up? And if yes, do I need to configure the relationship only in the associative mapping class, and how exactly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source