📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

SQL Server rejects your migration with 'may cause cycles or multiple cascade paths'. What does it mean and how do you fix it?

Intermediate Theory Intermediate

🎯 Key points

  • SQL Server error 1785: multiple cascade paths or a cycle
  • EF Core cascades required relationships by default
  • Pick the owning path; set the other to Restrict or NoAction (both ON DELETE NO ACTION on SQL Server)
  • ClientCascade deletes only tracked dependents
  • Delete dependents explicitly (ExecuteDelete) in the use case
  • Soft delete: IsDeleted or DeletedAt + global query filter
  • Large cascades lock many rows and bloat the log

⏱ 30-second answer

It is SQL Server error 1785: SQL Server refuses a foreign key with ON DELETE CASCADE when the table can already be reached by another cascade path, or the cascade would form a cycle. EF Core makes required relationships cascade by default, so a model like Company → Projects → Tasks plus Company → Employees → Tasks triggers it. Fix it by choosing which path really owns the delete and setting the other relationship to DeleteBehavior.Restrict or DeleteBehavior.NoAction (both emit ON DELETE NO ACTION on SQL Server; ClientCascade is an option if EF Core should delete loaded dependents itself). Then delete the dependents explicitly in the use case, often with ExecuteDelete, or switch to soft delete: an IsDeleted flag or DeletedAt column plus a global query filter, so nothing is physically removed. Wide cascades also lock many rows in one transaction, so avoiding them helps performance.

🗣 Full answer (senior)

How I would answer this in an interview

"SQL Server only allows one cascading path into a table. If deleting a Company could reach Tasks through both Projects and Employees, it rejects the second ON DELETE CASCADE with error 1785 rather than guess the order. EF Core hits this because it makes every required relationship cascade by convention.

The fix is a domain decision, not a syntax one: which parent owns the child? Usually Project owns Task, and Employee is only an assignee. So I keep cascade on Project → Task and set Employee → Task to DeleteBehavior.Restrict (or NoAction; on SQL Server both generate ON DELETE NO ACTION, the difference is only in how EF Core treats tracked entities). Removing an employee then means reassigning or deleting their tasks explicitly in the handler.

Often the better answer is not deleting at all. Soft delete marks the row with IsDeleted or DeletedAt, a global query filter hides it, and history stays intact for audit. Physical cascades over big graphs also hold locks on every child row in one transaction, which is a performance risk."


Options

Behaviour SQL generated What EF Core does with tracked dependents
Cascade ON DELETE CASCADE Marks them Deleted
Restrict / NoAction ON DELETE NO ACTION Throws or leaves FK; you handle it
ClientCascade ON DELETE NO ACTION Deletes loaded dependents only
SetNull ON DELETE SET NULL Nulls the FK (optional relationships)
modelBuilder.Entity<TaskItem>()
    .HasOne(t => t.Assignee)
    .WithMany(e => e.Tasks)
    .OnDelete(DeleteBehavior.Restrict);

Soft delete in one line each

  • Column: public DateTimeOffset? DeletedAt { get; set; }
  • Filter: modelBuilder.Entity<TaskItem>().HasQueryFilter(t => t.DeletedAt == null);
  • An interceptor turns Deleted state into an update that sets DeletedAt.

Production example

A cascade from Tenant deleted [~2 million] rows in one transaction and blocked writes for [4 minutes]; we moved to soft delete plus a nightly batched purge with ExecuteDelete in chunks of [5,000].

🔍 What the interviewer is checking

Checks practical knowledge of SQL Server cascade rules.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me