Entity Framework Core Interview Question #93
What are composite keys in EF Core?
Migrations, Database Schema & Data Management Mid-Level Intermediate
Quick Interview Answer
A composite key uses more than one property to uniquely identify an entity. It is common in explicit join entities and some legacy database schemas.
Detailed Explanation
A composite key uses more than one property to uniquely identify an entity. It is common in explicit join entities and some legacy database schemas.
Composite primary keys are normally configured with the Fluent API. The order and meaning of the key properties should be stable.
For many business entities, a single surrogate key can simplify references, but composite keys remain appropriate when the combination naturally identifies the row.
Code Example
modelBuilder.Entity<StudentCourse>()
.HasKey(x => new
{
x.StudentId,
x.CourseId
});