Entity Framework Core Interview Question #32

When should you use an explicit join entity for a many-to-many relationship?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

Use an explicit join entity when the relationship has its own data or behavior. Examples include enrollment date between Student and Course, quantity between Order and Product, or assigned role between User and Project.

Detailed Explanation

Use an explicit join entity when the relationship has its own data or behavior. Examples include enrollment date between Student and Course, quantity between Order and Product, or assigned role between User and Project.

The join entity normally contains foreign keys to both principal entities plus the additional relationship properties.

This approach makes the relationship itself a first-class part of the model and gives full control over indexes, constraints, queries, and updates.

Code Example

public class StudentCourse
{
    public int StudentId { get; set; }
    public Student Student { get; set; } = null!;

    public int CourseId { get; set; }
    public Course Course { get; set; } = null!;

    public DateTime EnrolledAtUtc { get; set; }
}