【问题标题】:Get Model List Json using Pomelo.EntityFrameworkCore.MySql in Asp.Net Core在 Asp.Net Core 中使用 Pomelo.EntityFrameworkCore.MySql 获取模型列表 Json
【发布时间】:2018-11-16 03:31:42
【问题描述】:

如何使用Pomelo返回MenuList JSON string?当我在Model 中添加MenuList 时,我会收到一条错误消息,显示“未知列菜单列表”。因为在我的MySql Menus.tbl 中没有MenuList 列。

请给我任何解决方案,在此先感谢!

菜单.cs

public class Menus
{
    [Key]
    public int MenuId { set; get; }
    public string MenuName { set; get; }
    public int? ParentId { set; get; }
    public int ActiveNo { set; get; }
    public List<Menus> MenuList { set; get; }
}

菜单控制器.cs

    [HttpGet]
    public ActionResult<List<Menus>> GetMenus()
    {
        List<Menus> menuList = new List<Menus>();

        foreach (Menus m in _context.menusss.ToList())
        {
            menuList.Add(m);
        }

        List<Menus> menuTree = GetMenuTree(menuList, null);
        return menuTree;
    }

    private List<Menus> GetMenuTree(List<Menus> list, int? parentId)
    {
        return list.Where(x => x.ParentId == parentId).Select(x => new Menus()
        {
            MenuId = x.MenuId,
            MenuName = x.MenuName,
            ParentId = x.ParentId,
            ActiveNo = x.ActiveNo,
            MenuList = GetMenuTree(list, x.MenuId)
        }).ToList();
    }

【问题讨论】:

  • 我刚刚尝试了您的代码,但无法重现。我想原因与[NotMapped] 有关。你能告诉我们更多细节吗?另外,如果我理解正确,您应该使用List&lt;Menus&gt; MenuList 而不是Menus MenuList
  • @itminus 请查看我的 MenusController.cs,我正在尝试获取这样的 JSON 字符串。

标签: mysql list asp.net-core model pomelo-entityframeworkcore-mysql


【解决方案1】:

我找到了解决方案,只需添加 `[NotMapped]' 以忽略该属性。

public class Menus
{
    [Key]
    public int MenuId { set; get; }
    public string MenuName { set; get; }
    public int? ParentId { set; get; }
    public int ActiveNo { set; get; }

    [NotMapped]
    public List<Menus> MenuList { set; get; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2020-10-24
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多