我有一个 MySQL 数据库,其中的表都在一条直线上相互关联。
移交 -> 版 block -> 条目 -> 回复
我想对这些表的 JSON 数组执行单个 http 请求。
目前在我的 Controller 中使用以下内容:
public JsonResult GetHandovers()
{
using (tidrapportEntities context = new tidrapportEntities())
{
context.Configuration.LazyLoadingEnabled = false;
var result2 = context.handovers
.Include(x => x.handoversections
.Select(z => z.handoverentries
.Select(y => y.handoverreplies)))
//.Where(p => p.Country == "Sweden")
.Select(w => new
{
Country = w.Country,
Id = w.HandOverId,
Sections = w.handoversections
}).ToList();
return Json(result2, JsonRequestBehavior.AllowGet);
}
}
但最终,这并没有返回所有层的数据。
[
{
"Country": "Sweden",
"Id": 1,
"Sections": [
{
"handoverentries": [],
"handovers": null,
"SectionId": 3,
"HandOverId": 1,
"Title": "Misc"
},
{
"handoverentries": [],
"handovers": null,
"SectionId": 2,
"HandOverId": 1,
"Title": "OOH"
},
{
"handoverentries": [],
"handovers": null,
"SectionId": 1,
"HandOverId": 1,
"Title": "DOH"
}
]
}
]
如何加载所有 4 个表并将它们放在一个 JSON 中?我会接受任何方法,只要我能让它工作!
最佳答案
我从这段代码中了解到,您希望获得 Handovers
、HandoverSections
、HandoverEntries
和 HandoverReplies
.Include(x => x.handoversections
.Select(z => z.handoverentries
.Select(y => y.handoverreplies)))
使用 Include
时,您可以提供一个表达式,如上所示:Include(x => x.handoversections)
但重载之一是字符串。您可以像这样使用它:.Include("School.Classes.Pupils")
。在你的情况下,我认为它会像这样:
context.handovers
.Include("handoversections.handoverentries")
.Include("handoversections.handovers")
请记住,您可以为所有需要的导航属性链接 Include
。
关于mysql - 我可以使用导航属性加载一个包含表的整个数据库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31029054/