c# - Adding role with UserManager -
i trying build simple method (just test how works), grant me (current user) new role (i have 1 role now, can simple query)
public actionresult index() { using (var db = new mydbcontext()) { var manager = new usermanager<applicationuser>(new userstore<applicationuser>(db)); var id = user.identity.getuserid(); var role = db.aspnetroles.first(); manager.addtorole(id, role.name); } return view(); }
i getting error: the entity type applicationuser not part of model current context.
on manager.addtorole(id, role.name);
line. novice asp.net don't know infrastructure, code mentioned lot of times (here, on stack) in questions user registration.
by way, goal, have implement role system project database-first migration system, manager.addtorole(id, role.name);
code add records in db?
below stub (incomplete), shows adding newly created user role:
dbcontext context = db; identityresult ir; var um = new usermanager<applicationuser>( new userstore<applicationuser>(context)); applicationuser au = new applicationuser(); au = db.users.firstordefault(u => u.username == uname); if (au == null) { // add user var user = new applicationuser() { username = trustnoone("name", uname), email = trustnoone("email", uemail), properusername = trustnoone("propername", upropname) }; // create password user ir = um.create(user, upass); // assign user role if (user.isinrole(urole) != true) { ir = um.addtorole(user.id, urole); } }
please note, uname, uemail, upropname, upass, , urole parameters passed method code lives. also, can ignore trustnoone bit (it's use).
Comments
Post a Comment