【发布时间】:2019-07-08 06:43:54
【问题描述】:
我使用 Ef Core 2.1,在其中我启用了软删除查询过滤器。
在某些情况下,我想从实体中检索软删除的导航属性,但我无法检索数据(导航属性为空,因为它已被软删除)。
我用这个doc(2017年写的)作为参考,据说
过滤器不能包含对导航属性的引用。
我想知道是否有任何方法可以启用这种行为。
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 会使代码更加复杂和不可维护。
【问题讨论】:
-
你试过
.IgnoreQueryFilters()吗? -
查看帖子的编辑版本。我曾尝试使用 IgnoreQueryFilters,但它似乎确实适用于嵌套属性。
-
您需要包含导航属性。
dbContext.Forms.Include(f => f.Sprint).IgnoreQueryFilters().FirstOrDefault(f => f.Id == 1).Sprint -
如果我已经使用了延迟加载代理,这有必要吗?
-
您是否尝试包含导航属性?只是好奇需要延迟加载的用例是什么?
标签: c# .net-core entity-framework-core soft-delete