Soft deletion feature is important in some cases that you need a second step to delete the data in your database. When you first delete it from your C# LinQ query, the data will only be marked as Deleted
but not delete in your database.
Multi-tenancy is one of the ASP.NET boilerplate's important features. But how can we implement the soft-deletion feature with only pure Entity Framework Core?
First, create an example entity like this:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public bool IsDeleted { get; set; }
}
The Id of the post will be your primary key in your database. And the IsDeleted
is for if the item is treated as a deleted item. Deleted items will not be selected. But you can still see it if your write SQL manually.
Create your DBContext
like this:
public class BloggingContext : DbContext
{
public BloggingContext()
{
}
public DbSet<Post> Posts { get; set; }
}
To configure your SQL Server connection, override the default OnConfiguring
like this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Demo.QueryFilters;Trusted_Connection=True;ConnectRetryCount=0;");
}
When the developer is trying to select blogs from the DBContext
, we need to add a filter to get only blogs not deleted. Override the default OnModelCreating
method.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>().HasQueryFilter(p => !p.IsDeleted);
}
If the developer is deleting an item from the table, we need to help him set the IsDeleted
and prevent deleting it in the database.
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
foreach (var item in ChangeTracker.Entries<Post>().Where(e => e.State == EntityState.Deleted))
{
item.State = EntityState.Modified;
item.CurrentValues["IsDeleted"] = true;
}
return base.SaveChanges();
}
Now we have a soft-deletion DBContext. With a BloggingContext
, developers can keep writing LINQ or lambda expression like he previously did and desn' need to concern about the deletion context for our context can handle entity deletion automatically.
With soft-deletion enabled, your data will never be dropped. So there might be more and more data in your database as time goes on. So you'd better configure another service to do the backup and deletion job.
And with soft-deletion, the cascaded deletion may stop working because the rules work on the database side. You have to manually delete cascaded entities.
I just finished reading your blog post on implementing soft deletion in Entity Framework Core, and I wanted to share my thoughts and feedback.
First, I appreciate your clear and concise explanation of the soft deletion concept and its importance in certain scenarios. Your step-by-step guide on how to implement this feature using pure Entity Framework Core is very helpful and easy to follow.
One of the most significant aspects of your blog post is the detailed code snippets you provided. This not only helps readers understand the concept better but also gives them a starting point for implementing the feature in their projects. Your explanation of how to create a soft-deletion DBContext and modify the
SaveChanges()
method to handle the deletion process automatically is a great addition.However, there are a few areas where I think the blog post could be improved:
It would be beneficial to provide a brief introduction to Entity Framework Core and the ASP.NET boilerplate at the beginning of the post. This would help readers who may not be familiar with these technologies better understand the context of the blog post.
While you mentioned the potential issue of database size growth due to soft deletion, it would be helpful to provide more information on how to handle this situation. For example, you could discuss strategies for archiving or permanently deleting data after a certain period.
You briefly mentioned the issue with cascaded deletion not working with soft deletion. It would be beneficial to provide more details on how to handle this situation, such as implementing custom logic to handle cascaded deletion manually.
Overall, your blog post is informative and well-written. I believe that with the suggested improvements, it could become an even more valuable resource for developers looking to implement soft deletion in Entity Framework Core. Keep up the great work!