向 ASP.Net 身份添加角色时出错
Error in adding roles to ASP.Net Identity
我的代码是这样的
namespace DiagnosisApp.Models
{
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DiagnosisContext")
{
}
}
}
namespace DiagnosisApp.DAL
{
public class DiagnosisInitializer : DropCreateDatabaseIfModelChanges<DiagnosisContext>
{
protected override void Seed(DiagnosisContext context)
{
var userManager = new UserManager<ApplicationUser>(new
UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new
RoleStore<IdentityRole>(context));
string name = "Admin";
string password = "123456";
//Create Role Admin if it does not exist
if (!roleManager.RoleExists(name))
{
IdentityResult roleresult = roleManager.Create(new IdentityRole(name));
}
//Create User=Admin with password=123456
var user = new ApplicationUser();
user.UserName = name;
IdentityResult adminresult = userManager.Create(user, password);
//Add User Admin to Role Admin
if (adminresult.Succeeded)
{
IdentityResult result = userManager.AddToRole(user.Id, name);
}
base.Seed(context);
}
}
}
namespace DiagnosisApp.DAL
{
public class DiagnosisContext : DbContext
{
public DiagnosisContext()
: base("DiagnosisContext")
{
Database.SetInitializer(new DiagnosisInitializer());
}
}
}
我觉得一切正常,但这会引发错误
The entity type IdentityRole is not part of the model for the current context.
第
行
if (!roleManager.RoleExists(name))
谁能指出我做错了什么?
检查 DiagnosisContext class 是否具有如下有效签名:
public class DiagnosisContext : IdentityDbContext<MyUser>
或
public class DiagnosisContext : IdentityDbContext
你的种子方法
protected override void Seed(DiagnosisContext context)
正在接受仅继承自普通 DbContext 的 DiagnosisContext
。你应该传入 ApplicationDbContext
protected override void Seed(ApplicationDbContext context)
DbContext 上没有 IdentityRole。
我的代码是这样的
namespace DiagnosisApp.Models
{
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DiagnosisContext")
{
}
}
}
namespace DiagnosisApp.DAL
{
public class DiagnosisInitializer : DropCreateDatabaseIfModelChanges<DiagnosisContext>
{
protected override void Seed(DiagnosisContext context)
{
var userManager = new UserManager<ApplicationUser>(new
UserStore<ApplicationUser>(context));
var roleManager = new RoleManager<IdentityRole>(new
RoleStore<IdentityRole>(context));
string name = "Admin";
string password = "123456";
//Create Role Admin if it does not exist
if (!roleManager.RoleExists(name))
{
IdentityResult roleresult = roleManager.Create(new IdentityRole(name));
}
//Create User=Admin with password=123456
var user = new ApplicationUser();
user.UserName = name;
IdentityResult adminresult = userManager.Create(user, password);
//Add User Admin to Role Admin
if (adminresult.Succeeded)
{
IdentityResult result = userManager.AddToRole(user.Id, name);
}
base.Seed(context);
}
}
}
namespace DiagnosisApp.DAL
{
public class DiagnosisContext : DbContext
{
public DiagnosisContext()
: base("DiagnosisContext")
{
Database.SetInitializer(new DiagnosisInitializer());
}
}
}
我觉得一切正常,但这会引发错误
The entity type IdentityRole is not part of the model for the current context.
第
行 if (!roleManager.RoleExists(name))
谁能指出我做错了什么?
检查 DiagnosisContext class 是否具有如下有效签名:
public class DiagnosisContext : IdentityDbContext<MyUser>
或
public class DiagnosisContext : IdentityDbContext
你的种子方法
protected override void Seed(DiagnosisContext context)
正在接受仅继承自普通 DbContext 的 DiagnosisContext
。你应该传入 ApplicationDbContext
protected override void Seed(ApplicationDbContext context)
DbContext 上没有 IdentityRole。