How can I automatically filter out soft deleted entities with Entity Framework?

Use EntityFramework.DynamicFilters. It allows you to create global filters that will be applied automatically (including against navigation properties) when queries are executed.

There is an example “IsDeleted” filter on the project page that looks like this:

modelBuilder.Filter("IsDeleted", (ISoftDelete d) => d.IsDeleted, false);

That filter will automatically inject a where clause on any query against an entity that is ISoftDelete. Filters are defined in your DbContext.OnModelCreating().

Disclaimer: I’m the author.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)