Entity Framework Core Interview Question #47

What is DeleteBehavior in EF Core?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

DeleteBehavior controls how EF Core and the database handle a relationship when a principal entity is deleted or a relationship is severed.

Detailed Explanation

DeleteBehavior controls how EF Core and the database handle a relationship when a principal entity is deleted or a relationship is severed.

Common choices include Cascade, Restrict, NoAction, SetNull, and client-side variants. The exact database behavior depends on the provider and whether the behavior can be represented by the database constraint.

Choose delete behavior according to business rules and database integrity requirements rather than simply accepting defaults without review.

Code Example

modelBuilder.Entity<Product>()
    .HasOne(p => p.Category)
    .WithMany(c => c.Products)
    .HasForeignKey(p => p.CategoryId)
    .OnDelete(DeleteBehavior.Restrict);