asp.net mvc - Entity Framework using Identity - Trying to update a user in the AspNetUsers table -
i having hard time figuring out how update/modify user in aspnetusers
table using entity framework , identity. getting error says:
"the entity type manageruserviewmodel not part of model current context."
this make sense, because not mention manageruserviewmodel
in applicationdbcontext
using. should create new context similar applicationdbcontext
, use one? or somehow add manageruserviewmodel
applicationdbcontext
?
here controller method doing updating when 'save' clicked:
public actionresult edituser([bind(include = "userid, email, username, roles, roleid, rolename")] manageruserviewmodel model) { if (modelstate.isvalid) { using (var context = new applicationdbcontext()) { var store = new userstore<applicationuser>(context); var manager = new usermanager<applicationuser>(store); applicationuser user = new applicationuser(); user.email = model.email; user.username = model.username; if (model.userid == "") { // since didn't have userid, assume new user task.waitany(manager.createasync(user, "password12")); } else { // since ef doesn't know product (it instantiated // modelbinder , not ef itself, need tell ef // object exists , modified copy of existing row context.entry(model).state = entitystate.modified; task.waitany(manager.updateasync(user)); } if (model.rolename != null && model.rolename != "") { task.waitany(manager.addtoroleasync(model.userid, model.rolename)); } task.waitany(context.savechangesasync()); return redirecttoaction("controlpanel"); } } return view(model); }
and here manageruserviewmodel:
public class manageruserviewmodel { public string userid { get; set; } public string email { get; set; } public string username { get; set; } [display(name = "role(s)")] public ienumerable<string> roles { get; set; } public string roleid { get; set; } public string rolename { get; set; } public ienumerable<selectlistitem> rolelist { get; set; } public static ienumerable<selectlistitem> getroles(string id = "") { using (var db = new applicationdbcontext()) { list<selectlistitem> list = new list<selectlistitem>(); foreach (var role in db.roles) { selectlistitem sli = new selectlistitem { value = role.name, text = role.name}; list.add(sli); } return list; } } public static string getroleid(string role) { using (var db = new identitydbcontext()) { string roleid = db.roles.where(r => r.name == role).first().id; return roleid; } } }
and here applicationdbcontext:
public class applicationdbcontext : identitydbcontext<applicationuser> { public applicationdbcontext() : base("defaultconnection", throwifv1schema: false) { } public dbset<registerviewmodel> registerusers { get; set; } public static applicationdbcontext create() { return new applicationdbcontext(); } }
let me know if other code needed.
edit: based on steve's comment, tried creating new context.
public class manageuserdbcontext : identitydbcontext<applicationuser> { public manageuserdbcontext() : base("defaultconnection", throwifv1schema: false) { } public dbset<manageruserviewmodel> users { get; set; } public static manageuserdbcontext create() { return new manageuserdbcontext(); } }
and in controller method, switched using context new manageuserdbcontext()
. not error out, still not update user.
you got right. there no need use dbcontext update user when have usermanager there.
your code should :
public actionresult edituser([bind(include = "userid, email, username, roles, roleid, rolename")] manageruserviewmodel model) { if (modelstate.isvalid) { using (var context = new applicationdbcontext()) { var store = new userstore<applicationuser>(context); var manager = new usermanager<applicationuser>(store); applicationuser user = new applicationuser(); user.email = model.email; user.username = model.username; if (model.userid == "") { // since didn't have userid, assume new user task.waitany(manager.createasync(user, "password12")); } else { // here fetch existing user, update properties , call manager update user = manager.findbyid(model.userid) user.email = model.email; user.username = model.username; task.waitany(manager.updateasync(user)); } if (model.rolename != null && model.rolename != "") { task.waitany(manager.addtoroleasync(model.userid, model.rolename)); } task.waitany(context.savechangesasync()); return redirecttoaction("controlpanel"); } } return view(model); }
Comments
Post a Comment