我遇到了延迟加载的问题。我有以下 dbcontext。
public virtual DbSet<AccountGroupMst> AccountGroupMst {get; set;}
我已经启用了延迟加载。
services.AddDbContext<DBContext>(x =>
x.UseSqlServer(Configuration.GetConnectionString("Test"))
.UseLazyLoadingProxies());
模型,我有虚拟的,它是一个自引用表。
public class AccountGroupMst
{
[Key]
[Required]
public int AccountGroupId { get; set; }
[MaxLength(255)]
[StringLength(255)]
[Required]
public string AccountGroupName { get; set; }
[ForeignKey("ParentAccountGroupId")]
public int? ParentAccountGroupId { get; set; }
public virtual AccountGroupMst ParentGroup { get; set; }
}
我遇到的问题是,实体框架返回所有子项。
{
"data": {
"0": {
"parentGroup": {
"parentGroup": {
"parentGroup": null,
"accountGroupId": 1,
"name": "Test 2.1",
"parentAccountGroupId": null
},
"accountGroupId": 5,
"name": "Test 1.1",
"parentAccountGroupId": 1
},
"accountGroupId": 18,
"name": "Test",
"parentAccountGroupId": 5
}
}
}
我的理解是,如果启用延迟加载,则不应显示“测试 1.1 和测试 2.1”。如果我做错了什么,请告诉我。