Tutorials Entity Framework Core Tutorial

Soft Delete Pattern in EF Core

Soft Delete Pattern 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 25 of 100

Soft Delete Pattern in EF Core

Beginner ✓IntermediateAdvancedProfessional

Intermediate · 2 — Data & queries · ~6 min · Module 3: CRUD Operations

What is this?

Soft delete marks rows as deleted with a flag (IsDeleted, DeletedAt) instead of physical DELETE. HasQueryFilter automatically excludes them from normal queries.

Why should you care?

ShopNest must retain order and customer history for tax audits — soft delete hides catalog items while preserving referential integrity.

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 Product
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public bool IsDeleted { get; set; }
    public DateTime? DeletedAt { get; set; }
}

// DbContext:
modelBuilder.Entity<Product>().HasQueryFilter(p => !p.IsDeleted);

// "Delete":
var p = await _context.Products.FindAsync(id);
p!.IsDeleted = true;
p.DeletedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();

What happened?

  • HasQueryFilter adds WHERE IsDeleted = 0 to every query on Product.
  • Setting flags performs logical delete without breaking FK references from old orders.

Practice next

  1. Add IsDeleted and DeletedAt to Product.
  2. Configure HasQueryFilter and migrate.
  3. Soft-delete a product and confirm normal queries omit it.
  4. Add filtered unique index on Sku where IsDeleted = 0.
  5. build restore by clearing IsDeleted and DeletedAt.

Remember

Soft delete uses flags instead of DELETE. HasQueryFilter hides deleted rows globally. IgnoreQueryFilters bypasses filter intentionally.

ShopNest catalog retirement

Discontinued product soft-deleted but old OrderItems still reference it for invoice reprints.

Outcome: GST audit trail intact; storefront stops showing the listing.

Interview prep for this lesson

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

Junior PDF Detailed
How do you write a basic CRUD operation (Insert / Update / Delete / Read) in EF Core?
Short answer: // Create context.Users.Add(new User { Name = &quot;John&quot; }); context.SaveChanges(); // Read var user = context.Users.FirstOrDefault(u =&gt; u.Id == 1); // Update user.Name = &quot;Jane&quot;; context.…
Junior PDF Detailed
What is cascade delete, and how can it be configured?
Short answer: Cascade delete ensures that related entities are deleted when the parent entity is deleted. Configure with Fluent API: modelBuilder.Entity&lt;Blog&gt;() .HasMany(b =&gt; b.Posts) .WithOne(p =&gt; p.Blog) .O…
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