POST/CON Postman's biggest API conference ever from April 30 - May 1 in San Francisco, California! And right now, the Postman team is offering 30% off tickets until March 26.
POST/CON 24 is for anyone who works with APIs or whose business relies on APIs. If you attend, you'll earn a certificate and a Postman Badge!
ABP Framework is a complete infrastructure for creating software solutions with modern architectures based on the ASP.NET Core platform. It comes with production-ready components, modular architecture, and Domain-Driven Design.
To delete or not to delete, that is the question (pun intended).
The traditional way to remove information in a database is through a "hard delete." A hard delete permanently erases a record from the database table. While this seems straightforward, it presents a significant risk: once that data is gone, it's gone for good.
Instead of physically removing a record, a soft delete marks it as deleted, usually by setting a flag like IsDeleted to true. The record remains in the database, but it's effectively hidden from regular application queries.
Today, we'll dive into the details of how to implement soft deletes using EF Core. We'll discuss global query filters, explore efficient ways to handle soft-deleted data, and weigh the trade-offs.
What Is a Soft Delete?
A soft delete is a data persistence strategy that prevents the permanent deletion of records from your database. Instead of removing data from the database, a flag is set on the record, indicating it as "deleted."
This approach allows the application to ignore these records during normal queries. However, you can restore these records if necessary. Soft delete is also practical if you want to keep foreign key constraints in place. Soft delete is a "non-destructive" operation in contrast with hard delete, where data is completely removed from the database.
A hard delete uses the SQL DELETE statement:
DELETEFROM bookings.Reviews
WHERE Id =@BookingId;
A soft delete, on the other hand, uses an UPDATE statement:
UPDATE bookings.Reviews
SET IsDeleted =1, DeletedOnUtc =@UtcNowWHERE Id =@BookingId;
The data is still present in the database, and the operation can be undone.
But you need to remember to filter out soft-deleted data when querying the database:
SELECT*FROM bookings.Reviews
WHERE IsDeleted =0;
Let's see how we can implement soft delete with EF Core.
Soft Deletes Using EF Core Interceptors
EF Core interceptors provide a powerful mechanism for intercepting and modifying database operations. For example, you can intercept the saving changes operation to implement soft delete functionality.
Let's create an ISoftDeletable marker interface to represent soft-deletable entities:
The entities that should support soft delete will implement this interface. You will need to apply the respective database migration to create these columns.
The next component we need is a SaveChangesInterceptor, which allows us to hook into the SavingChangesAsync (or SavingChanges) method. We can access the ChangeTracker and look for entries that implement ISoftDeletable and are flagged for deletion. We can figure this out by checking if the entity state is EntityState.Deleted.
When we find the entities flagged for deletion, we loop through them and update their state to EntityState.Modified. You should also set the respective values for the IsDeleted and DeletedOnUtc properties. This will cause EF to generate an UPDATE operation instead of a DELETE operation.
If you want to learn more, here's an article with a few practical use cases for EF Core interceptors.
Automatically Filtering Soft-Deleted Data
To ensure that soft-deleted records are automatically excluded from queries, we can use EF Core global query filters. We can apply query filters to entities using the OnModelCreating method to automatically exclude records marked as deleted. This feature dramatically simplifies writing queries.
Here's how to configure the soft delete query filter:
To enhance query performance, especially in tables with a significant number of soft-deleted records, you can use filtered indexes. A filtered index only includes records that meet the specified criteria. This reduces the index size and improves query execution times for operations that exclude filtered records. Most popular databases support filtered indexes.
It's worthwhile to think through if you even need to soft delete records.
In enterprise systems, you're typically not thinking about "deleting" data. There are business concepts that don't involve deleting data. A few examples are canceling an order, refunding a payment, or voiding an invoice. These "destructive" operations return the system to a previous state. But from a business perspective, you aren't really deleting data.
Soft deletes are helpful if there is a risk of accidental deletion. They allow you to easily restore soft-deleted records.
In any case, consider if soft deletes make sense from a business perspective.
Takeaway
Soft deletes offer a valuable safety net for data recovery and can enhance historical data tracking. However, it's crucial to assess whether they truly align with your application's specific requirements. Consider factors like the importance of deleted data recovery, any auditing needs, and your industry's regulations. Creating a filtered index can improve query performance on tables with soft-deleted records.
If you decide that soft deletes are a good fit, EF Core provides the tools necessary for a streamlined implementation.
P.S. There are 2 ways I can help you improve your skills:
Modular Monolith Architecture
This in-depth course will transform the way you build monolith systems. You will learn the best practices for applying the Modular Monolith architecture in a real-world scenario.
This is the complete blueprint for building production-ready applications using Clean Architecture. Join 2,400+ other students to accelerate your growth as a software architect.
I love hearing from readers, and I'm always looking for feedback. How am I doing with The .NET Weekly? Is there anything you'd like to see more or less of? Which aspects of the newsletter do you enjoy the most?
Hit reply and say hello - I'd love to hear from you!
Stay awesome,
Milan
You received this email because you subscribed to our list. You can unsubscribe at any time.
What Is a Modular Monolith? Read on: my website / Read time: 6 minutes BROUGHT TO YOU BY Extend your existing Blazor Applications to Desktop and Mobile Bring the best of web UI to native client
Lightweight In-Memory Message Bus Using .NET Channels Read on: my website / Read time: 6 minutes BROUGHT TO YOU BY Iron Software's Suite Expansion: Unveiling 9 Developer Libraries. Iron Software
Automatically Register Minimal APIs in ASP.NET Core Read on: milanjovanovic.tech Read time: 4 minutes The .NET Weekly is brought to you by: Become a Postman master with Postman Intergalactic
Using Scoped Services From Singletons in ASP.NET Core Read on: milanjovanovic.tech Read time: 3 minutes The .NET Weekly is brought to you by: Become a Postman master with Postman
Getting the Current User in Clean Architecture Read on: milanjovanovic.tech Read time: 4 minutes The .NET Weekly is brought to you by: Blending AI with UI: Crafting LLM UIs in Blazor,
ISSUE #434 24th of November 2024 Hi Kotliners! Next week is the last one to send a paper proposal for the KotlinConf. We hope to see you there next year. Announcements State of Kotlin Scripting 2024
More Time to Write A fully functional clock that ticks backwards, giving you more time to write. Tech Stuff Martijn Faassen (FWIW I don't know how to use any debugger other than console.log) People
Also: Best Outdoor Smart Plugs, and More! How-To Geek Logo November 23, 2024 Did You Know After the "flair" that servers wore—buttons and other adornments—was made the butt of a joke in the
JSK Daily for Nov 23, 2024 View this email in your browser A community curated daily e-mail of JavaScript news React E-Commerce App for Digital Products: Part 4 (Creating the Home Page) This component
What (and who) video-based social media leaves out. Here's a version for your browser. Hunting for the end of the long tail • November 23, 2024 Not Ready For The Camera Why hasn't video
Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Microsoft. You are given an string representing the initial conditions of some dominoes.
These two maps compare the world's tallest countries, and the world's shortest countries, by average height. View Online | Subscribe | Download Our App TIME IS RUNNING OUT There's just 3
November 23, 2024 | Read Online Subscribe | Advertise Good Morning. Welcome to this special edition of The Deep View, brought to you in collaboration with Convergence. Imagine if you had a digital
Top Tech Content sent at Noon! How the world collects web data Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, November 23, 2024? The HackerNoon