Soft deletion marks a record as deleted instead of removing it. Application queries exclude marked records, while the data remains stored. This is often safer and easier than physically deleting rows, although it does not suit every scenario. section 12.6 section 12.4.4
Implement the pattern by:
- Adding an IsDeleted Boolean property to the entity. section 12.4.4
- Setting IsDeleted to true in the delete operation, then saving the entity with SaveChangesAsync(). section 12.4.4
- Adding Where clauses to retrieval methods so deleted records cannot be returned through the application. section 12.4.4
Marking a recipe as deleted
public async Task DeleteRecipe(int recipeId)\n{\n var recipe = await _context.Recipes.FindAsync(recipeId);\n if (recipe is null)\n throw new Exception("Unable to find the recipe");\n\n recipe.IsDeleted = true;\n await _context.SaveChangesAsync();\n} section 12.4.4
Listing 12.10 Marking entities as deleted in EF Core
public async Task DeleteRecipe(int recipeId)
{
var recipe = await _context.Recipes.FindAsync(recipeId); #1
if(recipe is null) { #2
throw new Exception("Unable to find the recipe"); #2
} #2
recipe.IsDeleted = true; #3
await _context.SaveChangesAsync(); #4
}
Shows the complete EF Core operation for locating an entity, marking it deleted, and saving the change. section 12.4.4

