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

c# entity-framework entity-framework-core

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

1
2
2/15/2018 11:27:39 PM

Accepted Answer

May you can use Non-Tracking Query for this type progress.

Her is the link for this. https://docs.microsoft.com/en-us/ef/core/querying/tracking

A simple usage, may you have to add a few things. I did not debug this code.

GetAllWithoutFilter

Context.Set<TEntity>().AsNoTracking().IgnoreQueryFilters();

GetAllAsNoTracking

Context.Set<TEntity>();

public virtual IQueryable<TEntity> GetAllWithoutFilter(string[] include)
{
    IQueryable<TEntity> query = Context.Set<TEntity>().AsNoTracking().IgnoreQueryFilters();

    if (include != null)
    {
        query = include.Aggregate(query, (current, includePath) => current.Include(includePath));
    }

    return query;
}


public virtual IQueryable<TEntity> GetAll(string[] include)
{
    IQueryable<TEntity> query =  Context.Set<TEntity>();

    if (include != null)
    {
        query = include.Aggregate(query, (current, includePath) => current.Include(includePath));
    }

    return query;
}
3
2/16/2018 7:46:17 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow