Skip to main content

In this article, we will explore how to configure complex entity relationships in Entity Framework Core using configuration classes to maintain a clean and maintainable codebase.

---
title: Comprehensive Guide to Configuring Entity Relationships in Entity Framework Core
subtitle: In this article, we will explore how to configure complex entity relationships in Entity Framework Core using configuration classes to maintain a clean and maintainable codebase.
author: Jon LaBelle
date: October 16, 2024
source: https://jonlabelle.com/snippets/view/markdown/comprehensive-guide-to-configuring-entity-relationships-in-entity-framework-core
notoc: false
---

This article provides a comprehensive example for configuring complex entity
relationships in Entity Framework Core using configuration classes.

We'll being by defining various entities (`Student`, `Course`, `Enrollment`,
`Instructor`, `Department`) and their relationships, including one-to-many,
many-to-one, one-to-one, and many-to-many.

You'll learn how to use configuration classes to maintain a clean and
maintainable `DbContext`.

It also covers the concept of eager loading, explaining its importance in
reducing database calls and improving performance.

Let's jump in.

## Entity Relationships

Relationships help define how entities interact with each other in a relational database, ensuring data integrity and enabling complex queries.

### Entity Relationship Types

- **One-to-Many**: One entity is related to many instances of another entity.

- **Many-to-One**: Many instances of an entity are related to one instance of another entity.

- **One-to-One**: One entity is related to one instance of another entity.

- **Many-to-Many**: Many instances of an entity are related to many instances of another entity, often represented through a join table (like `Enrollment` below).

### Entity Relationships in this Example

Here's a brief summary of each relationship in the context of our example:

1. **One-to-Many**

   - **Department to Course**: A single `Department` can have multiple `Courses`. This means each course belongs to one department, but a department can offer many courses.

   - **Student to Enrollment**: A single `Student` can have multiple `Enrollments`. This means a student can enroll in many courses, but each enrollment record is for one student.

2. **Many-to-One**

   - **Course to Department**: Each `Course` belongs to one `Department`. This is the inverse of the one-to-many relationship between `Department` and `Course`.

   - **Enrollment to Student**: Each `Enrollment` is associated with one `Student`. This is the inverse of the one-to-many relationship between `Student` and `Enrollment`.

   - **Enrollment to Course**: Each `Enrollment` is associated with one `Course`. This means each enrollment record is for one course, but a course can have many enrollments.

3. **One-to-One**

   - **Department to Instructor (Administrator)**: Each `Department` has one `Instructor` as its administrator. This means a department can have only one administrator, and an instructor can administer only one department.

4. **Many-to-Many (Many-to-Many)**

   - **Course to Instructor**: A `Course` can be taught by multiple `Instructors`, and an `Instructor` can teach multiple `Courses`. This means there is a many-to-many relationship between courses and instructors.

   - **Student to Course (via Enrollment)**: A `Student` can enroll in multiple `Courses`, and a `Course` can have multiple `Students` enrolled. This many-to-many relationship is represented through the `Enrollment` entity.

And here's a textual representation of the entity relationships in the form of an entity map:

```plaintext
Student
  - StudentId (PK)
  - Name
  - Enrollments (1-to-Many with Enrollment)

Course
  - CourseId (PK)
  - Title
  - DepartmentId (FK)
  - Department (Many-to-1 with Department)
  - Enrollments (1-to-Many with Enrollment)
  - Instructors (Many-to-Many with Instructor)

Enrollment
  - EnrollmentId (Composite PK)
  - StudentId (FK)
  - Student (Many-to-1 with Student)
  - CourseId (FK)
  - Course (Many-to-1 with Course)

Instructor
  - InstructorId (PK)
  - Name
  - DepartmentId (FK)
  - Department (Many-to-1 with Department)
  - Courses (Many-to-Many with Course)

Department
  - DepartmentId (PK)
  - Name
  - InstructorId (FK)
  - Administrator (1-to-1 with Instructor)
  - Courses (1-to-Many with Course)
```

## Code Implementation

### 1. Define the Entities

Define the entities (`Student`, `Course`, `Enrollment`, `Instructor`, `Department`).

```csharp
using System.Collections.Generic;

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public ICollection<Enrollment> Enrollments { get; set; } = [];
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }
    public int DepartmentId { get; set; }
    public Department Department { get; set; }
    public ICollection<Enrollment> Enrollments { get; set; } = [];
    public ICollection<Instructor> Instructors { get; set; } = [];
}

public class Enrollment
{
    public int EnrollmentId { get; set; }
    public int StudentId { get; set; }
    public Student Student { get; set; }
    public int CourseId { get; set; }
    public Course Course { get; set; }
}

public class Instructor
{
    public int InstructorId { get; set; }
    public string Name { get; set; }
    public int DepartmentId { get; set; }
    public Department Department { get; set; }
    public ICollection<Course> Courses { get; set; } = [];
}

public class Department
{
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public int InstructorId { get; set; }
    public Instructor Administrator { get; set; }
    public ICollection<Course> Courses { get; set; } = [];
}
```

### 2. Create Configuration Classes

Create configuration classes for each entity.

```csharp
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class StudentConfiguration : IEntityTypeConfiguration<Student>
{
    public void Configure(EntityTypeBuilder<Student> builder)
    {
        builder.HasKey(s => s.StudentId);

        builder.Property(s => s.Name)
            .IsRequired()
            .HasMaxLength(50);

        builder.HasMany(s => s.Enrollments)
            .WithOne(e => e.Student)
            .HasForeignKey(e => e.StudentId);
    }
}

public class CourseConfiguration : IEntityTypeConfiguration<Course>
{
    public void Configure(EntityTypeBuilder<Course> builder)
    {
        builder.HasKey(c => c.CourseId);

        builder.Property(c => c.Title)
            .IsRequired()
            .HasMaxLength(100);

        builder.HasMany(c => c.Enrollments)
            .WithOne(e => e.Course)
            .HasForeignKey(e => e.CourseId);

        builder.HasMany(c => c.Instructors)
            .WithMany(i => i.Courses);

        builder.HasOne(c => c.Department)
            .WithMany(d => d.Courses)
            .HasForeignKey(c => c.DepartmentId);
    }
}

public class EnrollmentConfiguration : IEntityTypeConfiguration<Enrollment>
{
    public void Configure(EntityTypeBuilder<Enrollment> builder)
    {
        builder.HasKey(e => new { e.StudentId, e.CourseId });

        builder.HasOne(e => e.Student)
            .WithMany(s => s.Enrollments)
            .HasForeignKey(e => e.StudentId);

        builder.HasOne(e => e.Course)
            .WithMany(c => c.Enrollments)
            .HasForeignKey(e => e.CourseId);
    }
}

public class InstructorConfiguration : IEntityTypeConfiguration<Instructor>
{
    public void Configure(EntityTypeBuilder<Instructor> builder)
    {
        builder.HasKey(i => i.InstructorId);

        builder.Property(i => i.Name)
            .IsRequired()
            .HasMaxLength(50);

        builder.HasMany(i => i.Courses)
            .WithMany(c => c.Instructors);

        builder.HasOne(i => i.Department)
            .WithMany(d => d.Courses)
            .HasForeignKey(i => i.DepartmentId);
    }
}

public class DepartmentConfiguration : IEntityTypeConfiguration<Department>
{
    public void Configure(EntityTypeBuilder<Department> builder)
    {
        builder.HasKey(d => d.DepartmentId);

        builder.Property(d => d.Name)
            .IsRequired()
            .HasMaxLength(50);

        builder.HasOne(d => d.Administrator)
            .WithOne()
            .HasForeignKey<Department>(d => d.InstructorId);

        builder.HasMany(d => d.Courses)
            .WithOne(c => c.Department)
            .HasForeignKey(c => c.DepartmentId);
    }
}
```

### 3. Apply the Configurations in the DbContext

Apply the configurations in the `DbContext`.

```csharp
using Microsoft.EntityFrameworkCore;

public class SchoolContext : DbContext
{
    public DbSet<Student> Students { get; set; }
    public DbSet<Course> Courses { get; set; }
    public DbSet<Enrollment> Enrollments { get; set; }
    public DbSet<Instructor> Instructors { get; set; }
    public DbSet<Department> Departments { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration(new StudentConfiguration());
        modelBuilder.ApplyConfiguration(new CourseConfiguration());
        modelBuilder.ApplyConfiguration(new EnrollmentConfiguration());
        modelBuilder.ApplyConfiguration(new InstructorConfiguration());
        modelBuilder.ApplyConfiguration(new DepartmentConfiguration());
    }
}
```

### 4. Use Eager Loading to Select Related Entities

[Eager loading](https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager)
is a technique in Entity Framework Core that allows you to load related entities
as part of the initial query, reducing the number of database calls and
improving performance. It should be used to avoid the "N+1" query problem, where
multiple queries are executed to retrieve related data, which can lead to
significant performance issues.

```csharp
using System.Linq;

public class SchoolService
{
    private readonly SchoolContext _context;

    public SchoolService(SchoolContext context)
    {
        _context = context;
    }

    public void LoadData()
    {
        var students = _context.Students
            .Include(s => s.Enrollments)
                .ThenInclude(e => e.Course)
                    .ThenInclude(c => c.Department)
            .Include(s => s.Enrollments)
                .ThenInclude(e => e.Course)
                    .ThenInclude(c => c.Instructors)
            .ToList();

        var courses = _context.Courses
            .Include(c => c.Department)
            .Include(c => c.Instructors)
            .Include(c => c.Enrollments)
                .ThenInclude(e => e.Student)
            .ToList();

        var instructors = _context.Instructors
            .Include(i => i.Courses)
            .Include(i => i.Department)
            .ToList();

        var departments = _context.Departments
            .Include(d => d.Courses)
            .Include(d => d.Administrator)
            .ToList();
    }
}
```