TestBike logo

Ef query soft delete. NET using Entity Framework Core. NET 7 with a query ...

Ef query soft delete. NET using Entity Framework Core. NET 7 with a query filter. But it's not working. Today, we'll dive into the details of how to implement soft deletes using EF Core. Mar 16, 2024 · A soft delete is a data persistence strategy that prevents the permanent deletion of records from your database. Jul 10, 2017 · Implementing the soft-delete pattern with Entity Framework Core 2. Step by step tutorial with GitHub project. Global query filters that hide deleted rows automatically, partial indexes that keep queries fast, an admin bypass for auditing, cascade behaviour on related entities, and an interceptor that sets DeletedAt on SaveChanges — with production-ready code throughout. When soft deleting, you might be tempted to add an isDeleted column to your table and model and update all of your queries to include a check for isDeleted == false, or something similar. Jan 12, 2024 · Learn to optimize C# EF Core with interceptors! Discover performance tracking for slow or long running SQL queries and implement soft deletes efficiently. Dec 6, 2017 · I've created an interface to try to do a soft delete, mixing shadow properties and query filters. One missed condition can cause real bugs. . Soft delete checks get duplicated. Sep 15, 2025 · Tired of IsDeleted flags being missed in queries? Learn how to use EF Core’s global query filters to enforce soft deletes automatically and prevent data leaks. Database-Per-Tenant: Dynamic DbContext Connection String Switching In the database-per-tenant model, there are no global query filters and no TenantId columns. Oct 23, 2025 · Learn how to implement a safe and efficient soft delete pattern in . May 13, 2025 · Learn how to implement soft deletes in EF Core using flags, query filters, SaveChanges overrides, and repositories for safer data handling. May 25, 2023 · Implement soft-delete features in . As applications grow, filters spread everywhere. Feb 24, 2026 · For soft-delete, use a filtered index (WHERE IsDeleted = 0) to avoid index bloat from deleted rows that normal queries never touch. 0 is very easy and can be transparent. You can add soft-delete to an existing model without changing your LINQ code. Jul 21, 2025 · Global query filters allow attaching a filter to an entity type and having that filter applied whenever a query on that entity type is executed; think of them as an additional LINQ Where operator that's added whenever the entity type is queried. The isolation boundary is the database itself. I've got soft delete working for all my entities and soft deleted items are not retrieved via the context using a technique suggested by this answer. Jan 29, 2019 · Soft Delete in EF Core Let’s shift gears and talk about Entity Framework Core. Tenant rules are easy to forget. Query filters are powerful tools in Entity Framework. Soft delete — Deleted flag with MarkAsDeleted and a global query filter that hides deleted rows transparently Optimistic concurrency — Version is decorated with [ConcurrencyCheck]; EF Core raises a concurrency exception on conflict automatically Aug 29, 2020 · In this article you will learn how to implement soft delete capability in your application and how would it impact on querying of data. Unfortunately I haven't worked out how to do this bit based on the entity deriving from an abstract class Jul 2, 2020 · Add code to your DbContext to apply a query filter to these entity classes How to set/reset Soft Delete In the next sections I describe these stages in detail. 1. Named Global Query Filters solve this. 4 days ago · Implement soft delete in EF Core 8 the right way. public interface IDeletableEntity {} And then in my model builder builder. I assume a typical EF Core class with normal read/write properties, but you can adapt it to other entity class styles, like Domain-Driven Design (DDD) styled entity classes. How do I run EF Core migrations safely in a multi-tenant shared database? 4 days ago · The same pattern applies to soft delete, audit fields, and any other cross-cutting concern you want applied uniformly across the model. Add an IsDeleted discriminator to every entity that can be soft deleted. Discover why it matters, how to use global query filters and the ClientCascade feature, explore the pros and cons, and see how to stay compliant with user privacy laws like GDPR. Such filters are useful in a variety of cases. Feb 20, 2026 · This document describes how Entity Framework Core database contexts are configured to support multi-tenancy at the data access layer. Jun 21, 2016 · One crucial aspect is to take into account the cascade deletion of related entities, and either disable cascade delete, or understand and control the cascade delete timing behavior of EF Core. For information about how tenant context is initially resolved from HTTP requests, see Tenant Resolution. It covers the BaseDbContext class, tenant-scoped database routing, and automatic query filtering. Jun 14, 2023 · This post will explore how to implement a Soft Delete strategy with Entity Framework Core and how to use it during writing and reading to your database engine of choice. That includes when you access the entity via navigation properties. yek dtj nkn mis vzq yzl stf twm wlq mtg icn sek vpk xkw xkn