SHM*_*HMT 3 c# soft-delete entity-framework-core .net-core
我使用 Ef Core 2.1,在其中启用了软删除查询过滤器。
在某些情况下,我想从实体中检索软删除导航属性,但无法检索数据(导航属性为 null,因为它被软删除)。
我使用这个文档(写于 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;
Run Code Online (Sandbox Code Playgroud)
作为旁注,我想声明我在 StartUp.cs 中使用了延迟加载代理
services.AddDbContext<ApplicationDbContext>(options =>
options.UseLazyLoadingProxies().UseSqlServer(connectionString));
Run Code Online (Sandbox Code Playgroud)
而不是使用'Include()'和'ThenInclude()',因为我的模型比这里给出的例子更复杂。使用 include 会使代码更加复杂和难以维护。
尝试这个
var data = DbContext.Set<Table>().IgnoreQueryFilters().ToList();
Run Code Online (Sandbox Code Playgroud)
或者
var data = DbContext.TableName.IgnoreQueryFilters().ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2815 次 |
| 最近记录: |