문제

IdentityDBContext aspnetroles에 추가 된 속성 / 필드에 대한 문제가 발생

IdentityDBContext를 사용하여 문제가 여기에 있습니다.

public class MyIdentityDb : IdentityDbContext<ApplicationUser>
{
    public IdentityDb()
        : base("IdentityDb")
    {
    }
}
.

하지만 설명하겠습니다 ...

IdentityUser에서 상속되는 AppludmentUser 클래스에 속성을 추가하여 AspNetUsers 테이블에 필드를 추가 할 수있는 것이 좋습니다. 예 :

public class ApplicationUser : IdentityUser
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    }
.

Sy Natural IdentityRole에서 상속되는 ApplicationRole 클래스에 속성을 추가하여 AspNetroles 테이블에 필드를 추가 할 수 있습니다. 예 :

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}
.

완벽한 작동합니다. 데이터베이스의 필드를 볼 수 있습니다. 데이터를 추가 할 수 있습니다. 예 :

RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new MyIdentityDb()));

var role = new ApplicationRole() { Name = name, ProperName = propername };
var result = await roleManager.CreateAsync(role);
.

그러나 이제는 데이터에 도달하려고 할 때 문제가 발생합니다. 예 :

viewmodel :

public class IndexViewModel
{
    public IList<ApplicationUser> Users { get; set; }
    public IList<ApplicationRole> Roles { get; set; }
}
.

컨트롤러에서 :

private MyIdentityDb myIdentityDb = new MyIdentityDb();
.

컨트롤러의 인덱스 메소드 :

public ViewResult Index(int? page)
{
     return View(new IndexViewModel
     {
          Users = myIdentityDb.Users.ToList(),
          Roles = myIdentityDb.Roles.ToList()
     });
}
.

오류는 "myidentitydb.roles.tolist ()"에 있습니다. "

암시 적으로 유형을 암시 적으로 변환 할 수 없습니다.

물론 다음 예제와 같은 IdentityRole을 사용하도록 ViewModel을 변경할 수는 있지만 aspnetroles 테이블의 새로운 "propername"필드에 도달 할 수 없습니다.

public class IndexViewModel
{
    public IList<ApplicationUser> Users { get; set; }
    public IList<IdentityRole> Roles { get; set; }
}
.

다른 DB 클래스를 만들고 IdentityUser 대신 IdentityRole 유형을 전달할 수 있습니다.

public class MyIdentityDb : IdentityDbContext<ApplicationUser>
{
    public MyIdentityDb()
        : base("MyIdentityDb")
    {
    }
}

public class MyIdentityRolesDb : IdentityDbContext<ApplicationRole>
{
    public MyIdentityRolesDb()
        : base("MyIdentityDb")
    {
    }
}
.

및 컨트롤러 변경 :

private MyIdentityDb myIdentityDb = new MyIdentityDb();
private MyIdentityRolesDb myIdentityRolesDb = new MyIdentityRolesDb();
.

컨트롤러에서 인덱스 메소드를 변경하십시오.

public ViewResult Index(int? page)
{
     return View(new IndexViewModel
     {
          Users = myIdentityDb.Users.ToList(),
          Roles = myIdentityRolesDb.Roles.ToList()
     });
}
.

그러나 우리는 같은 문제로 끝납니다. IdentityDBContext가 IdentityUser 유형을 가져 오는 사실입니다.

어떤 아이디어를 사용자 정의 필드 / 속성으로 역할 목록을 얻을 수있는 방법

도움이 되었습니까?

해결책

2.0.0 베타 패키지로 업그레이드하는 경우 역할 관리자에서 직접 ApplicationRole을 직접 얻을 수 있어야합니다.

roleManager.Roles
.

DB 컨텍스트에 드롭 다운 할 필요가 없으므로 2.0 릴리스에서 수정 된 1.0의 제한이었습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top