在 Asp.net Identity MVC 5 中创建角色 [英] Creating Roles in Asp.net Identity MVC 5

查看:24
本文介绍了在 Asp.net Identity MVC 5 中创建角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用新的 Asp.net 身份安全框架的文档很少.

There is very little documentation about using the new Asp.net Identity Security Framework.

我已经拼凑了我可以尝试创建的新角色并向其添加用户.我尝试了以下操作:在 ASP.NET 标识中添加角色

I have pieced together what I could to try and create a new Role and add a User to it. I tried the following: Add role in ASP.NET Identity

看起来它可能已经从这个博客获得了信息:使用 asp.net 身份构建一个简单的待办事项应用程序并将用户与待办事项相关联

which looks like it may have gotten the info from this blog: building a simple to-do application with asp.net identity and associating users with to-does

我已将代码添加到每当模型更改时运行的数据库初始化程序中.它在 RoleExists 函数上失败并出现以下错误:

I have added the code to a Database Initializer that is run whenever the model changes. It fails on the RoleExists function with the following error:

System.InvalidOperationException 发生在 mscorlib.dll实体类型 IdentityRole 不是当前上下文模型的一部分.

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));
    }
}

感谢任何帮助.

推荐答案

验证您具有以下 MyContext 类的签名

Verify you have following signature of your MyContext class

公共类 MyContext : IdentityDbContext

或者

公共类 MyContext : IdentityDbContext

代码对我有用,没有任何修改!!!

这篇关于在 Asp.net Identity MVC 5 中创建角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆