Web 中的延迟加载 API Core 2.2
Lazy load in Web API Core 2.2
我遇到延迟加载问题。我有以下 dbcontext。
public virtual DbSet<AccountGroupMst> AccountGroupMst {get; set;}
我开启了延迟加载
services.AddDbContext<DBContext>(x =>
x.UseSqlServer(Configuration.GetConnectionString("Test"))
.UseLazyLoadingProxies());
模型,我有虚拟,它是一个自引用 table。
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; }
}
我遇到的问题是,Entity framework returns 所有 children。
{
"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
}
}
}
我的理解是,如果启用延迟加载,则不应显示 'Test 1.1 and Test 2.1'。如果我做错了什么,请告诉我。
延迟加载只会在访问时获取值。当吐出 JSON 时,EF Core 会访问属性并检索值作为单独的查询。因此,不建议使用延迟加载 ASP.NET Core.
我遇到延迟加载问题。我有以下 dbcontext。
public virtual DbSet<AccountGroupMst> AccountGroupMst {get; set;}
我开启了延迟加载
services.AddDbContext<DBContext>(x =>
x.UseSqlServer(Configuration.GetConnectionString("Test"))
.UseLazyLoadingProxies());
模型,我有虚拟,它是一个自引用 table。
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; }
}
我遇到的问题是,Entity framework returns 所有 children。
{
"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
}
}
}
我的理解是,如果启用延迟加载,则不应显示 'Test 1.1 and Test 2.1'。如果我做错了什么,请告诉我。
延迟加载只会在访问时获取值。当吐出 JSON 时,EF Core 会访问属性并检索值作为单独的查询。因此,不建议使用延迟加载 ASP.NET Core.