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

查看:164
本文介绍了禁用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天全站免登陆