c# - 禁用导航属性的 softDelete 查询过滤器

标签 c# .net-core entity-framework-core soft-delete

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

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

我用了这个doc (2017年写的)作为引用,并注明

Filters cannot contain references to navigation properties.

我想知道是否有任何方法可以实现这种行为。

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 中使用了延迟加载代理

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

而不是使用“Include()”和“ThenInclude()”,因为我的模型比此处给出的示例更复杂。使用 include 会使代码更加复杂和难以维护。

最佳答案

试试这个

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

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

关于c# - 禁用导航属性的 softDelete 查询过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929590/

相关文章:

.net-core - 对于只有 FrameworkTarget 的项目,dotnet build ignore --framework 参数

c# - .NetCore 项目在 Windows 上的 JetBrains Rider 中加载失败

c# - Entity Framework 核心 : Adding an object with a List to a DbContext results in an empty List

azure-sql-database - 在 Entity Framework 7/MVC 6 上动态更改连接字符串(每个请求)

c# - ASP.NET 标识 : Generate random password

c# - 使用 IMAP 命令获取消息的大小

c# - 静态类与。具有私有(private)构造函数和所有静态属性和方法的类?

asp.net core 2.0 spa angular + 谷歌地图

c# - Entity Framework 工具不适用于 UWP 应用程序 C#

c# - 如何最大化进程吞吐量(C#)?