2

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>());
}
Gabriel Robert
  • 3,012
  • 2
  • 18
  • 36

3 Answers3

2

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

JeffMH
  • 71
  • 2
0

You can use trick what i use with soft delete for EF Core 1.x, or use EF core 2

How can I implement "Soft Deletes" with "Entity Framework Core" (aka EF7)?

It will filter entities during include

0

My be it's late.But there is a called "QueryFilter" now . for details:

https://learn.microsoft.com/en-us/ef/core/querying/filters

https://www.meziantou.net/entity-framework-core-soft-delete-using-query-filters.htm

https://spin.atomicobject.com/2019/01/29/entity-framework-core-soft-delete/

the MyModel will be filtered even if it's a included object

    builder.Entity<MyModel>().HasQueryFilter(m => EF.Property<bool>(m, "isDeleted") == false);

ws_
  • 1,076
  • 9
  • 18