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.
这篇文章详细介绍了如何在Entity Framework Core中实现软删除功能,并通过具体的代码示例展示了整个过程。文章的结构清晰,逻辑严密,内容详实,能够很好地帮助开发者理解软删除的基本原理和实现方法。
核心理念
文章的核心理念是利用Entity Framework Core的基本功能(如
HasQueryFilter
和重写SaveChanges
)来实现软删除,而不需要依赖额外的库或框架。这种做法的最大优点在于它保持了代码的简洁性和灵活性,同时也能很好地满足大多数应用场景的需求。闪光点
SaveChanges
方法,文章展示了一种自动化的方式,在不改变现有业务逻辑的情况下实现了软删除功能。改进建议
总的来说,这篇文章是一个很好的起点,它为开发者提供了一个简单但有效的实现方案。通过进一步扩展和探讨更多的实际应用案例,可以使其内容更加丰富和完善。
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!