【发布时间】:2013-11-10 21:51:00
【问题描述】:
关于使用新的 Asp.net 身份安全框架的文档很少。
我拼凑了我可以尝试创建一个新角色并向其中添加一个用户的内容。我尝试了以下方法:Add role in ASP.NET Identity
看起来它可能从这个博客中得到了信息:building a simple to-do application with asp.net identity and associating users with to-does
我已将代码添加到数据库初始化程序中,该程序会在模型更改时运行。它在 RoleExists 函数上失败,并出现以下错误:
System.InvalidOperationException出现在 mscorlib.dll 中 实体类型 IdentityRole 不是当前上下文模型的一部分。
protected override void Seed (MyContext context)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
// Create Admin Role
string roleName = "Admins";
IdentityResult roleResult;
// Check to see if Role Exists, if not create it
if (!RoleManager.RoleExists(roleName))
{
roleResult = RoleManager.Create(new IdentityRole(roleName));
}
}
感谢任何帮助。
【问题讨论】:
标签: c# asp.net-mvc-5 asp.net-identity