Tutorials ASP.NET Core Complete Tutorial (ShopNest)

EF Core Relationships — One-to-One, One-to-Many, Many-to-Many

Learn EF Core Relationships — One-to-One, One-to-Many, Many-to-Many in our free ASP.NET Core Complete Tutorial (ShopNest) series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
EF Core Relationships — One-to-One, One-to-Many, Many-to-Many — ShopNest
Article 17 of 75 · Module 2: Entity Framework Core · ShopNest School Management System
Target keyword: ef core relationships · Read time: ~30 min · .NET: 8 / 9 · Project: ShopNest School Management System

Introduction

ShopNest Academy (School Management) connects Students, Courses, and Teachers through EF Core relationships. Modeling them correctly avoids duplicate data, orphaned records, and slow queries.

After this article you will

  • Configure one-to-one, one-to-many, and many-to-many
  • Use navigation properties both directions
  • Apply Fluent API for cascade delete rules
  • Choose eager, lazy, or explicit loading
  • Detect and fix N+1 query problems

Prerequisites

Concept deep-dive

One-to-one

Student → StudentProfile (one profile per student). FK on dependent side.

public class Student
{
    public int Id { get; set; }
    public StudentProfile Profile { get; set; }
}
public class StudentProfile
{
    public int Id { get; set; }
    public int StudentId { get; set; }
    public Student Student { get; set; }
}

One-to-many

Teacher → many Courses. FK TeacherId on Course.

Many-to-many (.NET 6+)

Students ↔ Courses via implicit join table, or explicit Enrollment entity with Grade and EnrolledAt.

public class Enrollment
{
    public int StudentId { get; set; }
    public Student Student { get; set; }
    public int CourseId { get; set; }
    public Course Course { get; set; }
    public decimal? Grade { get; set; }
}

// Fluent API
modelBuilder.Entity<Enrollment>()
    .HasKey(e => new { e.StudentId, e.CourseId });

Cascade delete

OnDelete(DeleteBehavior.Cascade) — deleting Teacher deletes Courses? Usually Restrict on business FKs to prevent accidents. Cascade OK for owned child rows (OrderLines).

Loading strategies

StrategyHowRisk
Eager.Include()Large joins if over-include
LazyVirtual + proxyN+1 in loops
Explicitcontext.Entry().Collection().Load()Verbose but controlled

Hands-on — ShopNest School Management System

  1. Scaffold Student, Course, Teacher, Enrollment entities.
  2. Migration with FK constraints and Restrict on Teacher→Course.
  3. Student details page: Include Profile + Enrollments.ThenInclude(Course).
  4. Log SQL — verify single query vs N+1.

Common errors & best practices

  • Multiple cascade paths in SQL Server — use Restrict on one relationship.
  • Missing inverse navigation — EF can still work but Fluent config is clearer.
  • Many-to-many without join entity when you need extra columns on enrollment.

Interview questions

Q: N+1 problem?
A: 1 query for list + N queries for related data — fix with Include or projection.

Q: When explicit join entity?
A: When relationship has payload (Grade, EnrolledAt) or audit fields.

Q: Cascade delete safe?
A: Rarely on master data; OK for dependent detail rows owned by parent.

Summary

  • Model relationships with navigation properties + FKs
  • Fluent API controls cascade and join tables
  • Include eagerly; avoid lazy loading in ASP.NET request paths
  • Enrollment join entity for Student-Course grades

Previous: EF Core LINQ Queries
Next: EF Core Fluent API

FAQ

Does EF Core 8 still need join entity for many-to-many?

Optional — implicit join table works for simple M:N; use explicit entity when you need extra columns.

How detect N+1?

Enable sensitive data logging; count SQL statements per HTTP request.

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ASP.NET Core Complete Tutorial (ShopNest)
Course syllabus
Module 1: Foundations
Module 2: Entity Framework Core
Module 3: Dependency Injection & Middleware
Module 4: Authentication & Security
Module 5: Web API
Module 6: Advanced Architecture
Module 7: Testing
Module 8: Deployment & DevOps
Module 9: Real-World Projects
Module 10: Advanced Topics
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details