如何使用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();
}
答案 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; }
}