Get Model List Json using Pomelo.EntityFrameworkCore.MySql in Asp.Net Core

582 Views Asked by At

How to return MenuList JSON string using Pomelo? When i add a MenuList in my Model i will get an error showing me "Unknown Column MenuList". Because in my MySql Menus.tbl there is no MenuList column.

Any solution for me please, thanks in advance !

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

There are 1 best solutions below

0
On BEST ANSWER

I found a solution, just add `[NotMapped]' to ignore the property.

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