A composite primary key combines multiple columns so their values uniquely identify a record. In EF Core, it commonly identifies rows in many-to-many junction tables. section 4.3.3
Configure composite keys in an EF Core model by:
- Adding [Key] properties to each participating identifier, such as BoardGameId and DomainId. section 4.3.3
- Defining the combined key with HasKey in OnModelCreating, because composite-key support requires Fluent API configuration. section 4.3.3
- Applying the pattern to junction entities such as BoardGames_Domains and BoardGames_Mechanics, where each mapped relationship should be unique. section 4.3.3
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<BoardGames_Domains>()
.HasKey(i => new { i.BoardGameId, i.DomainId });
modelBuilder.Entity<BoardGames_Mechanics>()
.HasKey(i => new { i.BoardGameId, i.MechanicId });
}
The configuration combines BoardGameId with DomainId or MechanicId inside OnModelCreating. section 4.3.3







