0

我对 MVC 很陌生。我是工作角色经理。在此之前,我将用户 ID 数据类型从字符串更改为 int。现在,我想在我的网站中担任角色。我已经创建了页面并添加了代码来运行该部分。运行网站后,当我点击创建新用户角色时,我在后台收到以下错误。这是我的代码:

private List<SelectListItem> GetAllRolesAsSelectList()
    {
        List<SelectListItem> SelectRoleListItems =
            new List<SelectListItem>();

        var roleManager =
            new RoleManager<IdentityRole>(
                new RoleStore<IdentityRole>(new ApplicationDbContext()));

        var colRoleSelectList = roleManager.Roles.OrderBy(x => x.Name).ToList();

        SelectRoleListItems.Add(
            new SelectListItem
            {
                Text = "Select",
                Value = "0"
            });

        foreach (var item in colRoleSelectList)
        {
            SelectRoleListItems.Add(
                new SelectListItem
                {
                    Text = item.Name.ToString(),
                    Value = item.Name.ToString()
                });
        }

        return SelectRoleListItems;
    }

这是引发错误的行

var roleManager =
                new RoleManager<IdentityRole>(
                    new RoleStore<IdentityRole>(new ApplicationDbContext()));

这是我的 IdentityModel 页面代码:

public class CustomUserRole : IdentityUserRole<int> 
{
}
public class CustomUserClaim : IdentityUserClaim<int> 
{ 
}
public class CustomUserLogin : IdentityUserLogin<int> 
{
}
public class CustomRole : IdentityRole<int, CustomUserRole>, IRole<int>
{
    public CustomRole() : base() { }
    public CustomRole(string name) 
        : this()
    { 
        this.Name = name; 
    }
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
    CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public CustomUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}
public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
    public CustomRoleStore(ApplicationDbContext context)
        : base(context)
    {
    }
} 
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, 
int, CustomUserLogin, CustomUserRole, CustomUserClaim>  
{
    public ApplicationDbContext()
        : base("SMSGoConnection")
    {
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

帮助将不胜感激。

4

0 回答 0