none
How can I enable the QueryFilters again in EntityFramework Core after calling IgnoreQueryFilters? RRS feed

  • Question

  • Here is what I have, one `Application` instance that contains two `Tags`, one marked as `IsDeleted` and one not. My `Application` and `Tag` class all have an `IsDeleted` property on the model.

       
    public class Application   
    {
            public int Id { get; set; }        
            public bool IsDeleted { get; set; }        
            public ICollection<Tag> Tags { get; set; } = new List<Tag>();    
    }





    My `ApplicationConfiguration` looks like this

       
     public class ApplicationConfiguration : IEntityTypeConfiguration<Application>    
    {  
          public void Configure(EntityTypeBuilder<Application> builder)       
          {
                builder.ToTable("Application");            
                builder.HasQueryFilter(d => !d.IsDeleted);        
                builder.HasKey(d => d.Id);            
                builder.Property(d => d.Id).ValueGeneratedOnAdd();            
                builder.HasQueryFilter(d => !d.IsDeleted);        
          }   
    }


    As you can see the query filter is applied on `IsDeleted`. For `Document` and `Tag`, I have the same query filter applied 

        
    builder.HasQueryFilter(d => !d.IsDeleted);



    In my repository, I have this a `GetAll` method that looks like this

      
      public virtual IQueryable<TEntity> GetAll(string[] include, bool includeAllRecords = false)
        {
            IQueryable<TEntity> query = includeAllRecords ? Context.Set<TEntity>().IgnoreQueryFilters() : Context.Set<TEntity>();
    
            if (include != null)
            {
                query = include.Aggregate(query, (current, includePath) => current.Include(includePath));
            }
    
            return query;
        }



    See the `IgnoreQueryFilters` when I pass the Boolean parameter. Now I can make queries over the `ApplicationRepository`:

        var appsWithJustActiveTags = ApplicationRepository.GetAll(new[] { "Tags" }).ToList();
        var allTags = ApplicationRepository.GetAll(new[] { "Tags" }, true).ToList();



    Both queries return the correct values for the tags when I pass the Boolean parameter, on the first one I get the Application with the Tags that are not deleted and in the second one I get the same application with all the tags including the deleted one. However, if I change the order of the execution of this queries, then both return the same result

        var allTags = ApplicationRepository.GetAll(new[] { "Tags" }, true).ToList();
        var appsWithJustActiveTags = ApplicationRepository.GetAll(new[] { "Tags" }).ToList();


        
    Seems that EntityFramework Core disables the QueryFilters for all queries in the same Context if you call `Context.Set<TEntity>().IgnoreQueryFilters();` and any subsequent calls to a simple `Context.Set<TEntity>();` don't get the filters back again
    Thursday, February 15, 2018 11:50 PM

Answers

  • I resolve the problem using AsNoTracking() as showed below

    IQueryable<TEntity> query = includeAllRecords ? Context.Set<TEntity>().AsNoTracking().IgnoreQueryFilters() : Context.Set<TEntity>();
    
    


    • Marked as answer by izinova Wednesday, February 21, 2018 12:54 AM
    Wednesday, February 21, 2018 12:54 AM

All replies

  • Hi izinova,

    >>Seems that EntityFramework Core disables the QueryFilters for all queries in the same Context if you call `Context.Set<TEntity>().IgnoreQueryFilters();` and any subsequent calls to a simple `Context.Set<TEntity>();` don't get the filters back again

    According to your description, it seems that you set a global IgnoreQueryFilters, which cause the issue, I don't suggest to do that.

    If you want to enable the QueryFilters, please try the extension as below.

    https://stackoverflow.com/questions/45096799/filter-all-queries-trying-to-achieve-soft-delete/45097532#45097532

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 16, 2018 2:23 AM
    Moderator
  • I resolve the problem using AsNoTracking() as showed below

    IQueryable<TEntity> query = includeAllRecords ? Context.Set<TEntity>().AsNoTracking().IgnoreQueryFilters() : Context.Set<TEntity>();
    
    


    • Marked as answer by izinova Wednesday, February 21, 2018 12:54 AM
    Wednesday, February 21, 2018 12:54 AM