Tutorials Entity Framework Core Tutorial

Self Referencing Relationships in EF Core

Self Referencing Relationships in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.

On this page

Entity Framework Core Tutorial · Lesson 49 of 100

Self Referencing Relationships in EF Core

Beginner ✓Intermediate ✓AdvancedProfessional

Advanced · 3 — Production skills · ~6 min · Module 5: Relationships

What is this?

Self referencing relationships point an entity to others of the same type — Category with ParentCategoryId, Employee with ManagerId.

Why should you care?

ShopNest category tree (Electronics → Laptops → Gaming) uses self-reference instead of separate table per level.

See it live — copy this example

Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public int? ParentCategoryId { get; set; }
    public Category? Parent { get; set; }
    public ICollection<Category> Children { get; set; } = new List<Category>();
}

modelBuilder.Entity<Category>()
    .HasOne(c => c.Parent)
    .WithMany(c => c.Children)
    .HasForeignKey(c => c.ParentCategoryId)
    .OnDelete(DeleteBehavior.Restrict);

What happened?

  • ParentCategoryId nullable FK allows root categories.
  • HasOne/WithMany on same entity type configures hierarchy.
  • Restrict prevents deleting parent with children.

Practice next

  1. Seed root and child categories.
  2. Query roots: Where(c => c.ParentCategoryId == null).
  3. Recursive CTE or client tree build for full hierarchy display.
  4. Load three levels with ThenInclude(c => c.Children).ThenInclude(c => c.Children).
  5. Add path column materialized for faster breadcrumb queries.

Remember

Same entity type for parent and child. Nullable FK marks root nodes. Restrict delete on hierarchies usually safest.

ShopNest category tree

Mega menu builds from self-referencing Category rows with ParentCategoryId.

Outcome: Unlimited depth aisles without schema change per level.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
What are the types of relationships in relational databases?
Short answer: EF Core supports the standard types of relationships: One-to-One (1:1) – One entity has one related entity. One-to-Many (1:N) – One entity relates to many others. Many-to-Many (M:N) – Many entities relate t…
Mid PDF Detailed
How to handle required vs optional relationships?
Short answer: In Fluent API: Required: .HasRequired(p =&gt; p.Blog) Optional: .HasOne(p =&gt; p.Blog) .WithMany() .IsRequired(false); EF infers: Reference type (e.g., Blog) → optional by default Non-nullable value type F…
Junior PDF Detailed
What is Entity Framework Core?
Short answer: Entity Framework Core (EF Core) is a modern, lightweight, cross-platform, open-source ORM (Object-Relational Mapper) for .NET. It allows developers to interact with databases using C# objects rather than SQ…
Mid PDF Detailed
What are the main differences between Entity Framework (EF6) and EF Core?
Short answer: Feature EF6 EF Core Platform .NET Framework only Cross-platform (.NET Core) Performance Slower Faster and more optimized LINQ support Limited Enhanced Change tracking Basic More efficient Migrations Availab…
Junior PDF Detailed
What is ORM (Object-Relational Mapping)?
Short answer: How does EF Core implement it? ORM is a technique that maps objects in code to relational database tables. EF Core implements ORM by mapping .NET classes (entities) to database tables using DbContext, DbSet…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Tutorial
Course syllabus

Entity Framework Core Tutorial

Module 1: EF Core Fundamentals
Module 2: Code First Approach
Module 3: CRUD Operations
Module 4: LINQ
Module 5: Relationships
Module 6: Advanced EF Core
Module 7: Performance Optimization
Module 8: Enterprise Architecture
Module 9: Testing & Debugging
Module 10: Real-World Projects
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