I am using the entity framework and modelling a many-to-many relationship.
I have created the relationship between the two entities using the fluent API (let\'s say
No, you can't. If you want to have access to the join table via a separate entity you must replace your many-to-many relationship by two one-to-many relationships and change the navigation properties in User
and Group
to refer to GroupMember
:
public class Group
{
public int GroupId { get; set; }
public virtual ICollection Members { get; set; }
}
public class User
{
public int UserId { get; set; }
public virtual ICollection Members { get; set; }
}
modelBuilder.Entity()
.HasMany(g => g.Members)
.WithRequired(gm => gm.Group);
modelBuilder.Entity()
.HasMany(u => u.Members)
.WithRequired(gm => gm.User);
Why do you want this GroupMember
entity? It doesn't contain any business meaning and has only references and keys. Usually you can get and modify any content of the join table by writing LINQ queries and by using the Group
and User
DbSets/entities and their navigation properties.