mysql - 我可以使用导航属性加载一个包含表的整个数据库吗?

标签 mysql json asp.net-mvc entity-framework

我有一个 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 中?我会接受任何方法,只要我能让它工作!

最佳答案

我从这段代码中了解到,您希望获得 HandoversHandoverSectionsHandoverEntriesHandoverReplies

.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/

相关文章:

javascript - ajax 加载后 setInterval javascript 调用不会消失

mysql - Apache2 Perl vHosts 错误

javascript - 正在加载自定义论坛帖子 : php echo vs. javascript.createElement

javascript - 无法从我的网络服务器读取本地服务器上的 json 文件

javascript - 使用空白 MVC 路由和 HttpGet 请求 Controller

asp.net-mvc - 在html.actionlink上,单击转到上一页

php - 我如何比较 $_POST ["fname"],.... 的结果与表中的记录?

php - mysql prepared statements中的绑定(bind)过程

javascript - 如何将 JSON 序列化对象转换为 JS 对象,以便可以访问其属性?

javascript - 如何使用 Angular-Translate 显示翻译?