【问题标题】:userManagerAddToRole(User,Role) : System.InvalidOperationException:RoleDoes"Random Role Id string" not existuserManagerAddToRole(User,Role) : System.InvalidOperationException:RoleDoes"Random Role Id string" 不存在
【发布时间】:2020-01-07 18:10:57
【问题描述】:

我是 Entity Framework 和 ASP.NET MVC 的新手。在我的项目中,我试图为用户分配一个角色。当我尝试为用户分配角色时,会生成此错误。即使我尝试在调试器中查看,我也得到了我期望的角色 ID 和用户 ID,但是当那些 id 传递给 userManager.AddToRole(User,Role) 时,它说角色不存在。

这是我创建角色的种子方法。这些角色正在AspNetRoles 表中创建。

internal sealed class Configuration : DbMigrationsConfiguration<VacationProjectV2.Models.ApplicationDbContext>
{
    private RoleManagement roleManager;
    private ApplicationDbContext dbContext;
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        dbContext = new ApplicationDbContext();
        roleManager = new RoleManagement(dbContext);
    }

    protected override void Seed(VacationProjectV2.Models.ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.C:\Users\brahm\source\repos\VacationProjectV2\VacationProjectV2\App_Start\
        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data.
        roleManager.CreateRole("Admin")   //.ToString().ToUpper();
        roleManager.CreateRole("ProjectManager")  //.ToString().ToUpper();
        roleManager.CreateRole("Developer") //.ToString().ToUpper();
    }
}

这是我尝试分配的 AdminController。

public class AdminsController : Controller
{
    ApplicationDbContext db = new ApplicationDbContext();
    UserManager<ApplicationUser> userManager;
    RoleManager<IdentityRole> roleManager;

    public AdminsController()
    {
        roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
        userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    }
    // GET: Admins

    public ActionResult AssignRole()
    {
        ViewBag.Users = new SelectList(db.Users, "Id", "UserName");
        ViewBag.Roles = new SelectList(db.Roles, "Id", "Name");
        return View();
    }

    [HttpPost]
    public ActionResult AssignRole(string Users,String Roles)
    {
        userManager.AddToRole(Users, Roles);
        ViewBag.Users = new SelectList(db.Users, "Id", "UserName");
        ViewBag.Roles = new SelectList(db.Roles, "Id", "Name");
        return View();
    }
    public ActionResult Index()
    {
        return View();
    }
}

这里是View 代码

@{
    ViewBag.Title = "AssignRoles";
}

<h2>AssignRoles</h2>
@using (Html.BeginForm())
{
    @Html.DropDownList("Users", "Select User");
    @Html.DropDownList("Roles","Select Role")
    <input type="Submit" value="Add Role"/>
}

所以我想在AspNetUserRoles 表中看到UserIdRoleId。我不知道我在哪里犯错。我尝试使用.Tostrong().ToUpper() 在种子方法中创建角色,方法是查看另一个具有相同问题的帖子,但它对我不起作用。请让我知道我在哪里犯了错误。

【问题讨论】:

  • 您确定可以将简单字符串与“userManager.AddToRole(Users, Roles);”一起使用?
  • 我不确定。我还没有学习 AddToRoleAsync 方法。所以我不能直接在 AddToRole 方法中添加字符串?
  • 你应该从你的帖子中记录什么是用户和角色。注意,AddToRole 和 AddToRoleAsync 是不一样的。你在用什么?
  • 我正在使用 AddToRole。非常感谢我发现我的错误我仔细检查了方法并且 AddToRole 需要 Role Name 的 Sting 而不是 Role Id 的字符串。现在它工作得很好谢谢@hugo

标签: c# asp.net-mvc entity-framework


【解决方案1】:

对不起伙计们这是我的错,我没有正确检查方法所需的参数。错误是userManager.AddToRole()。需要 roleName 而不是 roleId 作为参数。

 public ActionResult AssignRoles()
        {
            ViewBag.Users = new SelectList(db.Users, "Id", "UserName");
            ViewBag.Roles = new SelectList(db.Roles, "Name", "Name"); // Here I was Passing the Id insted Of Role Name.
            return View();
        }
        [HttpPost]
        public ActionResult AssignRoles(string Users,string Roles)
        {
            usersManager.AddToRole(Users, Roles);
            ViewBag.Users = new SelectList(db.Users, "Id", "UserName");
            ViewBag.Roles = new SelectList(db.Roles, "Name", "Name");
            return View();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 2015-05-18
    • 1970-01-01
    • 2022-12-12
    • 2021-04-29
    • 2019-02-13
    • 2019-03-06
    • 1970-01-01
    相关资源
    最近更新 更多