在Asp.Net Core中使用Pomelo.EntityFrameworkCore.MySql获取模型列表Json

时间:2018-11-16 03:31:42

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

如何使用MenuList JSON string返回Pomelo?当我在MenuList中添加Model时,将显示错误消息“ Unknown Column MenuList”。因为在我的MySql Menus.tbl中没有MenuList列。

请给我任何解决方案,谢谢!!

Menus.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; }
}

MenusController.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();
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,只需添加“ [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; }
}