How to access AspNetUserRoles table in ASP.NET MVC 5












-1















I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help










share|improve this question























  • what have you tried so far?

    – Simon Price
    Jan 2 at 23:01






  • 1





    Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?

    – Ghukas
    Jan 2 at 23:07











  • Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas

    – Alejandro1991
    Jan 2 at 23:11













  • I did not try anything just i want to access to this table from DB object in Controller @SimonPrice

    – Alejandro1991
    Jan 2 at 23:14













  • AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles; for each of the users.

    – Ghukas
    Jan 3 at 2:32
















-1















I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help










share|improve this question























  • what have you tried so far?

    – Simon Price
    Jan 2 at 23:01






  • 1





    Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?

    – Ghukas
    Jan 2 at 23:07











  • Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas

    – Alejandro1991
    Jan 2 at 23:11













  • I did not try anything just i want to access to this table from DB object in Controller @SimonPrice

    – Alejandro1991
    Jan 2 at 23:14













  • AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles; for each of the users.

    – Ghukas
    Jan 3 at 2:32














-1












-1








-1








I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help










share|improve this question














I use Identity 2 in my project and i Want display the list of AspNetUserRoles in the view. but i can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !?
Thanks in advance for your help







c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 23:00









Alejandro1991Alejandro1991

397




397













  • what have you tried so far?

    – Simon Price
    Jan 2 at 23:01






  • 1





    Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?

    – Ghukas
    Jan 2 at 23:07











  • Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas

    – Alejandro1991
    Jan 2 at 23:11













  • I did not try anything just i want to access to this table from DB object in Controller @SimonPrice

    – Alejandro1991
    Jan 2 at 23:14













  • AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles; for each of the users.

    – Ghukas
    Jan 3 at 2:32



















  • what have you tried so far?

    – Simon Price
    Jan 2 at 23:01






  • 1





    Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?

    – Ghukas
    Jan 2 at 23:07











  • Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas

    – Alejandro1991
    Jan 2 at 23:11













  • I did not try anything just i want to access to this table from DB object in Controller @SimonPrice

    – Alejandro1991
    Jan 2 at 23:14













  • AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles; for each of the users.

    – Ghukas
    Jan 3 at 2:32

















what have you tried so far?

– Simon Price
Jan 2 at 23:01





what have you tried so far?

– Simon Price
Jan 2 at 23:01




1




1





Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?

– Ghukas
Jan 2 at 23:07





Is it a code-first application? Do you have a database with that table in it and are there more than 2 columns it that table?

– Ghukas
Jan 2 at 23:07













Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas

– Alejandro1991
Jan 2 at 23:11







Yes it's code -first.and it Has two columns(RoleId and UserId) @Ghukas

– Alejandro1991
Jan 2 at 23:11















I did not try anything just i want to access to this table from DB object in Controller @SimonPrice

– Alejandro1991
Jan 2 at 23:14







I did not try anything just i want to access to this table from DB object in Controller @SimonPrice

– Alejandro1991
Jan 2 at 23:14















AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles; for each of the users.

– Ghukas
Jan 3 at 2:32





AspNetUserRoles table is a mapping table. You can access the table using AspNetUser object or AspNetRole object. Assuming you want to show all users and their roles, you can do that by getting it from var userRoles = user.AspNetRoles; for each of the users.

– Ghukas
Jan 3 at 2:32












2 Answers
2






active

oldest

votes


















1














This will give you a list of all users and their roles.



using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;

foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
};
}
}
}


I haven't tested the code, there can be a more optimal way.






share|improve this answer
























  • thanks alot this way is nice @Ghukas

    – Alejandro1991
    Jan 3 at 14:46



















1














You need to import the following model on the view where you want to retrieve the User Roles.



@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>   


Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.






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%2f54014290%2fhow-to-access-aspnetuserroles-table-in-asp-net-mvc-5%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This will give you a list of all users and their roles.



    using(var context = new YourContextName())
    {
    var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
    var users = context.AspNetUsers;

    foreach(var user in users)
    {
    foreach(var role in user.Roles)
    {
    usersAndRoles.Add(new UserRoleModel
    {
    UserName = user.UserName,
    RoleName = role.Name
    };
    }
    }
    }


    I haven't tested the code, there can be a more optimal way.






    share|improve this answer
























    • thanks alot this way is nice @Ghukas

      – Alejandro1991
      Jan 3 at 14:46
















    1














    This will give you a list of all users and their roles.



    using(var context = new YourContextName())
    {
    var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
    var users = context.AspNetUsers;

    foreach(var user in users)
    {
    foreach(var role in user.Roles)
    {
    usersAndRoles.Add(new UserRoleModel
    {
    UserName = user.UserName,
    RoleName = role.Name
    };
    }
    }
    }


    I haven't tested the code, there can be a more optimal way.






    share|improve this answer
























    • thanks alot this way is nice @Ghukas

      – Alejandro1991
      Jan 3 at 14:46














    1












    1








    1







    This will give you a list of all users and their roles.



    using(var context = new YourContextName())
    {
    var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
    var users = context.AspNetUsers;

    foreach(var user in users)
    {
    foreach(var role in user.Roles)
    {
    usersAndRoles.Add(new UserRoleModel
    {
    UserName = user.UserName,
    RoleName = role.Name
    };
    }
    }
    }


    I haven't tested the code, there can be a more optimal way.






    share|improve this answer













    This will give you a list of all users and their roles.



    using(var context = new YourContextName())
    {
    var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
    var users = context.AspNetUsers;

    foreach(var user in users)
    {
    foreach(var role in user.Roles)
    {
    usersAndRoles.Add(new UserRoleModel
    {
    UserName = user.UserName,
    RoleName = role.Name
    };
    }
    }
    }


    I haven't tested the code, there can be a more optimal way.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 2:51









    GhukasGhukas

    423922




    423922













    • thanks alot this way is nice @Ghukas

      – Alejandro1991
      Jan 3 at 14:46



















    • thanks alot this way is nice @Ghukas

      – Alejandro1991
      Jan 3 at 14:46

















    thanks alot this way is nice @Ghukas

    – Alejandro1991
    Jan 3 at 14:46





    thanks alot this way is nice @Ghukas

    – Alejandro1991
    Jan 3 at 14:46













    1














    You need to import the following model on the view where you want to retrieve the User Roles.



    @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>   


    Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.






    share|improve this answer




























      1














      You need to import the following model on the view where you want to retrieve the User Roles.



      @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>   


      Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.






      share|improve this answer


























        1












        1








        1







        You need to import the following model on the view where you want to retrieve the User Roles.



        @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>   


        Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.






        share|improve this answer













        You need to import the following model on the view where you want to retrieve the User Roles.



        @model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>   


        Then you only have to do a foreach of the Model and take his Name attribute (item.Name) it will return and show e.g. Admin, Employee, etc.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 23:29









        Didac RiusDidac Rius

        363




        363






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014290%2fhow-to-access-aspnetuserroles-table-in-asp-net-mvc-5%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory