Search code examples
c#entity-framework-coreglobal-query-filter

Ignore Global Query Filter


I have this query that retrieves documents from a repo:

var kycDocs = await _kycDocRepo.GetAllAsync(a => a.CreatedBy == query.UserId &&
            (query.Profile.Role == UserRoleName.Customer ? (a.Status == KYCDocumentStatus.Approved) :
            a.Status != KYCDocumentStatus.UploadInProgress),
            ignoreFilters: query.Profile.Role != UserRoleName.Customer,
            select: a => new CustomerIdentityItemDTO
            {
                Id = a.Id,
                Type = a.Type,
                IdentityNumber = a.IdentityNumber,
                IssuedOn = a.IssuedOn,
                CreatedOn = a.CreatedOn,
                ExpireOn = a.ExpireOn,
                DocumentUrl = a.DocumentUrl,
                Status = a.Status,
                SubType = a.SubType,
                ExpiryStatus = CustomerHelpers.GetDocumentValidityStatus(a.Type, a.ExpireOn)
            });

This is the raw SQL query:

SET @__query_UserId_0 = 1214;

SELECT `k`.`Id`, `k`.`Type`, `k`.`IdentityNumber`, `k`.`IssuedOn`, `k`.`CreatedOn`, `k`.`ExpireOn`, `k`.`DocumentUrl`, `k`.`Status`, `k`.`SubType`
FROM `KYCDocument` AS `k`
WHERE NOT (`k`.`IsDeleted`) AND ((`k`.`CreatedBy` = @__query_UserId_0) AND (`k`.`Status` <> -1))

This global query filter is set on the AuditableEntity that the kycDocRepo inherits from

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

I am to display all documents both deleted except the ones where the status are UploadInProgress to users that are not customers basically admins.

The major issue is that the ignoreFilters is not been executed. Also I had tried some other approaches which only added another where clause where K is deleted.

What I want to do is to ignore the query filters and display the deleted documents.

I have tried this just to see if the query filter will be ignored

var kycDocs = await _kycDocRepo.GetAllAsync(a => a.CreatedBy == query.UserId &&
             (query.Profile.Role == UserRoleName.Customer ? (a.Status == KYCDocumentStatus.Approved) : true),
           
             ignoreFilters: ignoreQueryFilters,
             select: a => new CustomerIdentityItemDTO
             {
                 Id = a.Id,
                 Type = a.Type,
                 IdentityNumber = a.IdentityNumber,
                 IssuedOn = a.IssuedOn,
                 CreatedOn = a.CreatedOn,
                 ExpireOn = a.ExpireOn,
                 DocumentUrl = a.DocumentUrl,
                 Status = a.Status,
                 SubType = a.SubType,
                 ExpiryStatus = CustomerHelpers.GetDocumentValidityStatus(a.Type, a.ExpireOn)
             });

GetAllAsyncMethod

public async Task<IQueryable<T>> GetAllAsync(Expression<Func<T, bool>> filter, bool ignoreFilters = false, Expression<Func<T, object>> includeProperties = null, string includePropertiesAsString = null)
 {
     var entity = includeProperties != null ? LoadNavigationProperties(Entity, includeProperties) : LoadNavigationProperties(Entity, includePropertiesAsString);
     if (ignoreFilters)
     {
         entity.IgnoreQueryFilters();
     }

     return await Task.FromResult(entity.Where(filter).AsQueryable<T>());
 }

Solution

  • I was able to fix the issue by applying this after the query filter

        if (query.Profile.Role != UserRoleName.Customer && kycDocs.Any(a => a.Status != KYCDocumentStatus.UploadInProgress))
     {
         kycDocs = kycDocs.IgnoreQueryFilters();
     }