Search code examples
linqasp.net-mvc-5entity-framework-6soft-delete

How can I replicate the functionality of IgnoreQueryFilter() in EF6?


I'm currently involved in a legacy ASP.NET MVC 5 project utilizing EF 6. The project demands the selective exclusion of a global filter in certain actions to retrieve soft-deleted data. While researching, I came across the IgnoreQueryFilters functionality in EF Core, which unfortunately isn't available in EF6.

I'm seeking guidance on potential alternatives or methods to selectively bypass this global filter in my scenario.

Here is the code using in the ApplicationDbContext class:

modelBuilder.Filter("IsDeleted", (Projects p) => p.IsDeleted, false);

Solution

  • EF 6 does not natively support global filters. You are likely using the EF Dynamic Filters NuGet package, in which case you have created the filter with the "IsDeleted" name, so you can disable it one-off using:

    _context.DisableFilter("IsDeleted");
    
    var data = _context.Items.Where(...)...
    
    _context.EnableFilter("IsDeleted"); // Re-enable if you have other queries that should respect the filter.
    

    ... if you are using an injected DbContext. If you are scoping a new DbContext instance with a using block or such then the EnableFilter is not needed.