我使用 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/