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












0














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









share|improve this question
























  • I just tried your code, but fails to reproduce. I guess the reason is something related to [NotMapped]. Could you please show us more details? Also, if I understand correctly, you should use List<Menus> MenuList instead of Menus MenuList.
    – itminus
    Nov 20 at 5:29










  • @itminus Please look at my MenusController.cs, im trying to get a JSON string like this.
    – Teh Wayne
    Nov 21 at 1:44
















0














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









share|improve this question
























  • I just tried your code, but fails to reproduce. I guess the reason is something related to [NotMapped]. Could you please show us more details? Also, if I understand correctly, you should use List<Menus> MenuList instead of Menus MenuList.
    – itminus
    Nov 20 at 5:29










  • @itminus Please look at my MenusController.cs, im trying to get a JSON string like this.
    – Teh Wayne
    Nov 21 at 1:44














0












0








0







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









share|improve this question















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






mysql list asp.net-core model pomelo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 1:42

























asked Nov 16 at 3:31









Teh Wayne

94




94












  • I just tried your code, but fails to reproduce. I guess the reason is something related to [NotMapped]. Could you please show us more details? Also, if I understand correctly, you should use List<Menus> MenuList instead of Menus MenuList.
    – itminus
    Nov 20 at 5:29










  • @itminus Please look at my MenusController.cs, im trying to get a JSON string like this.
    – Teh Wayne
    Nov 21 at 1:44


















  • I just tried your code, but fails to reproduce. I guess the reason is something related to [NotMapped]. Could you please show us more details? Also, if I understand correctly, you should use List<Menus> MenuList instead of Menus MenuList.
    – itminus
    Nov 20 at 5:29










  • @itminus Please look at my MenusController.cs, im trying to get a JSON string like this.
    – Teh Wayne
    Nov 21 at 1:44
















I just tried your code, but fails to reproduce. I guess the reason is something related to [NotMapped]. Could you please show us more details? Also, if I understand correctly, you should use List<Menus> MenuList instead of Menus MenuList.
– itminus
Nov 20 at 5:29




I just tried your code, but fails to reproduce. I guess the reason is something related to [NotMapped]. Could you please show us more details? Also, if I understand correctly, you should use List<Menus> MenuList instead of Menus MenuList.
– itminus
Nov 20 at 5:29












@itminus Please look at my MenusController.cs, im trying to get a JSON string like this.
– Teh Wayne
Nov 21 at 1:44




@itminus Please look at my MenusController.cs, im trying to get a JSON string like this.
– Teh Wayne
Nov 21 at 1:44












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331011%2fget-model-list-json-using-pomelo-entityframeworkcore-mysql-in-asp-net-core%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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





    share|improve this answer


























      0














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





      share|improve this answer
























        0












        0








        0






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





        share|improve this 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; }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 7:31









        Teh Wayne

        94




        94






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331011%2fget-model-list-json-using-pomelo-entityframeworkcore-mysql-in-asp-net-core%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen