禁用导航属性的 softDelete 查询过滤器 [英] Disable softDelete Query filters for navigation properties

查看:28
本文介绍了禁用导航属性的 softDelete 查询过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Ef Core 2.1,在其中启用了软删除查询过滤器.

I use Ef Core 2.1, in which I enabled a soft-delete query filter.

在某些情况下,我想从实体中检索软删除的导航属性,但无法检索数据(导航属性为空,因为它被软删除).

In some cases, I want to retrieve from an entity, a soft-deleted navigation property, but I could not retrieve the data (The navigation property is null because it was soft deleted).

我使用了这个 doc(写在2017)作为参考,并声明

I used this doc (which was written in 2017) as a reference, and it is stated that

过滤器不能包含对导航属性的引用.

Filters cannot contain references to navigation properties.

我想知道是否有任何方法可以启用此类行为.

I wish to know If there is any way to enable such behaviour.

public class Form {

    public int Id { get; set; }

    public virtual Sprint Sprint {get; set;}
}

public class Sprint: ISoftDeleteable {

    public int Id { get; set; }

    public string Name {get; set;}
}

// Indicates that every model that implements this interface should use soft delete.
public interface ISoftDeleteable
{ 

}

 // Both statements have returned null.
 Sprint sprint = applicationDbContext.Forms.FirstOrDefault(f => f.Id == 1).Sprint;
 Sprint sprint = applicationDbContext.Forms.IgnoreQueryFilters().FirstOrDefault(f => f.Id == 1).Sprint;

作为旁注,我想声明我在 StartUp.cs 中使用了延迟加载代理

As a side note, I wish to state that I use a lazy loading proxy in StartUp.cs

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseLazyLoadingProxies().UseSqlServer(connectionString));

而不是使用'Include()'和'ThenInclude()',因为我的模型比这里给出的例子更复杂.使用 include 会使代码变得更加复杂且难以维护.

Instead of using using 'Include()' and 'ThenInclude()', Because my model, is more complex than the exmplle given here. Using include will make the code more complicated and unmaintainable.

推荐答案

试试这个

var data = DbContext.Set<Table>().IgnoreQueryFilters().ToList();

var data = DbContext.TableName.IgnoreQueryFilters().ToList();

这篇关于禁用导航属性的 softDelete 查询过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆