如何在.Net Core 2中为用户创建列表页面

时间:2018-12-25 04:26:57

标签: .net-core

我们扩展了身份角色以及用户:

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

    }
}

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

    }
}

我们正在尝试创建用户的剃刀页面列表以及他们的角色:

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

我们设法仅获得用户列表,但不确定在这种情况下如何包括角色。

请问指针吗?

1 个答案:

答案 0 :(得分:0)

首先,尝试避免使用Include函数的字符串重载。相反,使用lamda可以帮助您确保该属性存在。例如,在这种情况下,您的用户类首先不存在名为UserRoles的属性。其次,您尝试使用的语法通常用于一对多关系。请注意,用户和角色是多对多关系,并且身份上下文(您的dbcontext扩展名)具有UserRoles属性。您应该能够使用如下查询使所有用户加入其角色:

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;