我正在尝试自定义我的应用程序。但当我试图创建以下角色时,我遇到了一些问题:
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不是当前的模型的一部分上下文
这是我的应用程序上下文:
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");
}
}
a做错了什么?
今天我遇到了同样的问题,但使用了.edmx
模型。我应该更新模型并重建几次项目,神奇的是它开始了。