0

We extended the Identity Roles as well Users:

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AthlosifyWebArchery.Models
{
    public class ApplicationRole : IdentityRole
    {

        public ApplicationRole() : base() { }

        public ApplicationRole(string roleName) : base(roleName) { }

        public ApplicationRole(string roleName, string description, DateTime createdDate) : base(roleName)
        {
            base.Name = roleName;

            this.Description = description;
            this.CreatedDate = createdDate;
        }

        public string Description { get; set; }
        public DateTime CreatedDate { get; set; }

    }
}

and

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace AthlosifyWebArchery.Models
{
    public class ApplicationUser : IdentityUser
    {

        public ApplicationUser() : base() { }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Address { get; set; }

        public string Suburb { get; set; }

        public string State { get; set; }

        public string Postcode { get; set; }

        public string Country { get; set; }


        [InverseProperty("ApplicationUser")]
        public IList<HostApplicationUser> HostApplicationUsers { get; set; }

        [InverseProperty("HostApplicationCreatedUser")]
        public HostApplicationUser HostApplicationCreatedUser { get; set; }

        [InverseProperty("HostApplicationLastModifiedUser")]
        public HostApplicationUser HostApplicationLastModifiedUser { get; set; }

        [InverseProperty("ApplicationUser")]
        public IList<ClubApplicationUser> ClubApplicationUsers { get; set; }

        [InverseProperty("ClubApplicationCreatedUser")]
        public ClubApplicationUser ClubApplicationCreatedUser { get; set; }

        [InverseProperty("ClubApplicationLastModifiedUser")]
        public ClubApplicationUser ClubApplicationLastModifiedUser { get; set; }

    }
}

We are trying to create a Razor Page list of users as well their role:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using AthlosifyWebArchery.Data;
using AthlosifyWebArchery.Models;
using Microsoft.AspNetCore.Identity;

namespace AthlosifyWebArchery.Pages.Administrators.Users
{
    public class IndexModel : PageModel
    {
        private readonly AthlosifyWebArchery.Data.ApplicationDbContext _context;

        public IndexModel(AthlosifyWebArchery.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public List<ApplicationUser> User { get; private set; }

        public List<IdentityUserRole<string>> UsersRoles { get; set; }  // get my roles or context of user

        public List<IdentityRole> AllRoles { get; private set; }

        public async Task OnGetAsync()
        {
            User = _context.Users.Include("UserRoles").ToList();
            //UsersRoles = _context.UserRoles.ToList(); // get my roles or context of user
            //AllRoles = _context.Roles.ToList();
        }
    }
}

We managed to get just the user list BUT not sure on how to include the Roles in this case.

Any pointer please?

dcpartners
  • 4,354
  • 11
  • 43
  • 64
  • possible duplicate of https://stackoverflow.com/questions/51004516/net-core-2-1-identity-get-all-users-with-their-associated-roles/51005445 – Neville Nazerane Dec 27 '18 at 06:23

1 Answers1

0

Firstly, try to avoid using Include function's string overload. Using the lamda instead will help you be sure that the property exists. For instance, in this case, a property named UserRoles doesn't exist for your user class in the first place. Secondly, the syntax you are trying to use it generally used for a one to many relationships. Note that users and roles is a many to many relation and the identity context (that your dbcontext extended) has a UserRoles property for this. You should be able to get all users joined with their roles using a query like this:

IEnumerable<User> users =  from u in context.Users
                            from r in context.Roles
                            from ur in context.UserRoles
                            where u.Id == ur.UserId && ur.RoleId == r.Id
                            select u;
Neville Nazerane
  • 5,682
  • 3
  • 31
  • 68