【问题标题】:Activate SoftDelete data filter when including collections包含集合时激活 SoftDelete 数据过滤器
【发布时间】:2017-07-13 13:53:44
【问题描述】:

在使用GetAllIncludingInclude 方法时,有没有办法自动激活“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>());
}

【问题讨论】:

  • GetAllIncluding 已经过滤了已删除的行并且只获取未删除的实体 (github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/…)。不行吗?
  • @hikalkan 很奇怪,它对我不起作用。我正在使用最新的模块零模板。
  • 我猜你确实从 ISoftDelete 实现了 SocialPost。对吗?
  • 是的@AlperEbicoglu

标签: c# aspnetboilerplate


【解决方案1】:

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 没有可用于修改从包含生成的查询的钩子,至少我正在阅读。

所以,这意味着您需要自己进行查询来解决这个问题。您可以在以下链接中查看如何通过使用投影来过滤包含:

Filtering include items in LINQ and Entity Framework

【讨论】:

  • JeffMH 清楚地解释了这个问题。在 EF Core 中不支持软删除过滤器。
【解决方案2】:

您可以使用我在 EF Core 1.x 的软删除中使用的技巧,或使用 EF Core 2

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

它会在包含期间过滤实体

【讨论】:

    【解决方案3】:

    我迟到了。但是现在有一个叫做“QueryFilter”的东西。 详情:

    https://docs.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/

    MyModel 将被过滤,即使它是一个包含的对象

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      相关资源
      最近更新 更多