Search code examples
c#aspnetboilerplate

Activate SoftDelete data filter when including collections


Is there a way to activate the "IsSoftDelete" EF Core filter automatically when using the GetAllIncluding or Include methods?

public override Task<PublicationDto> Get(EntityDto<Guid> input)
{
    var entity = Repository
                .GetAllIncluding(x => x.SocialPosts)
                .FirstOrDefault(x => x.Id == input.Id);
     return Task.FromResult(entity.MapTo<PublicationDto>());
}

Solution

  • The EFCore version of ABP does not automatically filter anything but the root entity of a query. If you look at the implementation within the AbpRepositoryBase, ApplyFilters only looks at the entity that the query is based on, not anything Included.

    if (typeof(ISoftDelete).GetTypeInfo().IsAssignableFrom(typeof(TEntity)))
    {
        if (UnitOfWorkManager?.Current == null || UnitOfWorkManager.Current.IsFilterEnabled(AbpDataFilters.SoftDelete))
        {
            query = query.Where(e => !((ISoftDelete)e).IsDeleted);
        }
    }
    

    In the regular implementation of EF(using EF v6.x), they are using the DynamicFilters nuget package that handles this for them, but that plugin doesn't exist for EF Core. This is really a limitation of EF Core, more so than it is with ABP. EF Core doesn't have the hooks available to modify the query generated from the Include, at least that's what I am reading.

    So, all that means is you will need to do your own query to solve this problem. You can see how to filter includes through the use of projections in the following link:

    Filtering include items in LINQ and Entity Framework