很少有關於使用新的Asp.net身份安全框架的文檔。在Asp.net中創建角色身份MVC 5
我拼湊出了我可以嘗試創建新角色並將其添加到用戶的任務。我試過如下: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
occurred in mscorlib.dll The entity type IdentityRole is not part of the model for the current context.
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));
}
}
任何幫助表示讚賞。
謝謝大家的回覆。一切都在工作。檢查背景使我朝着正確的方向前進。當創建asp.net標識時,它會創建一個新的上下文(ApplicationDbContext),它擴展了IdentityDbContext。在我的代碼中,我引用了沒有擴展IdentityDbContext的原始上下文。如果其他人有這個問題,請檢查您的上下文並仔細檢查您的APP_DATA目錄,以確保您不會意外創建兩個數據庫。 – colbyJax