通过预先加载获取所有列

Get all columns by eager loading

我创建了一个 table 与 ApplicationUser 的关系,当我想使用预加载时,我无法从 table 我基于用户的搜索中获取所有列验证。

var UserSites = await _SqldbContext.Users
                                   .Where(x => x.UserName == User.Identity.Name)
                                   .Include(x => x.sites)
                                   .ToListAsync() ;
return Json(UserSites);

但在 return 中我只得到其中的一行 table 两列

[{"sites":[{"id":1,"userId":"c0e8be95-535c-449c-9aa1-06702cd4c983"

但是我有更多行 userId 而且这里我只有两列但是我有两列以上,我不确定这里有什么问题请帮助我。

我认为 ToListAsync() 工作正常,如果你设置断点,你可能会看到你网站的所有数据,但我认为 Json 没有正确显示它,而不是从用户 [=16= 开始] 在您的示例中,我从您建议的 table 名称站点开始(以获取此 table 的所有数据)

 var UserSites = await _SqldbContext.sites.Include(x => x.[Name Of User in relation table]).Where(y=>y.[Name Of User in relation table].UserName== User.Identity.Name)
                .ToListAsync();


        List<sites> siteObject=new List<sites>();

        UserSites.ForEach(x =>
        {
            sites site = new sites() {
            // fill property of your class
            };

            siteObject.Add(site);
        });


        return Json(siteObject);

希望这段代码对你有用。