我的RoleManager在上下文中出错

时间:2016-05-06 02:15:19

标签: c# asp.net-mvc asp.net-identity

我正在尝试自定义我的应用程序。但是当我尝试创建跟随角色时,我遇到了一些问题:

private void createRolesandUsers()
{
    ApplicationDbContext context = new ApplicationDbContext();

    var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
    var UserManager = new KnockdocUserManager(new UserStore<Usuario, Papel, int, Login, UsuarioPapel, Claim>(context));

    if (!roleManager.RoleExists("Admin"))
    {
        //create Admin Role First
        var role = new IdentityRole();
        role.Name = "Admin";
        roleManager.Create(role);

错误:

  

实体类型IdentityRole不是当前模型的一部分   上下文。

这是我的appcontext:

public class ApplicationDbContext: IdentityDbContext<Usuario, Papel, int, Login, UsuarioPapel, Claim>
{
    public ApplicationDbContext()
        : base("BD_KnockdocContext")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Map Entities to their tables.
        modelBuilder.Entity<Usuario>().ToTable("Usuario");
        modelBuilder.Entity<Papel>().ToTable("Papel");
        modelBuilder.Entity<Claim>().ToTable("Claim");
        modelBuilder.Entity<Login>().ToTable("Login");
        modelBuilder.Entity<UsuarioPapel>().ToTable("UsuarioPapel");

        // Set AutoIncrement-Properties
        modelBuilder.Entity<Usuario>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Claim>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Papel>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        // Override some column mappings that do not match our default
        modelBuilder.Entity<Usuario>().Property(r => r.UserName).HasColumnName("Login");
        modelBuilder.Entity<Usuario>().Property(r => r.PasswordHash).HasColumnName("Password");
    }
}

出了什么问题?

1 个答案:

答案 0 :(得分:0)

今天我遇到了同样的问题但使用了.edmx模型。我应该更新模型并重建几次项目,神奇地开始。