c# - 包含集合时激活 SoftDelete 数据过滤器

标签 c# aspnetboilerplate

使用 GetAllInclusionInclusion 方法时,是否可以自动激活“IsSoftDelete”EF Core 过滤器?

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>());
}

最佳答案

ABP 的 EFCore 版本不会自动过滤除查询的根实体之外的任何内容。如果您查看 AbpRepositoryBase 中的实现,ApplyFilters 只会查看查询所基于的实体,而不是包含的任何内容。

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

在 EF 的常规实现中(使用 EF v6.x),他们使用 DynamicFilters nuget 包来处理此问题,但 EF Core 不存在该插件。这确实是 EF Core 的限制,比 ABP 的限制更严重。 EF Core 没有可用于修改从 Include 生成的查询的 Hook ,至少我正在阅读的是这样的。

因此,这意味着您需要执行自己的查询来解决此问题。您可以在以下链接中了解如何通过使用投影来过滤包含内容:

Filtering include items in LINQ and Entity Framework

关于c# - 包含集合时激活 SoftDelete 数据过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45082759/

相关文章:

c# - 如何使用 Linq To Twitter 获得 n 个关注者

c# - 使用 ASP.NET 样板将 ValueObject 存储在数据库中

c# - 在 Entity Framework Core 的系统版本时态表中查询数据

c# - 无法在 ASP.NET Zero 中启用实体历史记录

c# - 在 ASP.NET 样板中为应用程序服务配置审计日志记录

C# MYSQL datagridview 中的多表

c# - 未在全局处理程序中捕获/处理 ContinueWith 中引发的未处理异常

c# - 将字典序列化到磁盘?

c# - RedirectToAction 不更改 url

asp.net-mvc - Aspnet 样板/Aspnet 零慢 (IoC)