asp.net mvc 5 - The entity type IdentityRole is not part of the model for the current context -
i having trouble implementing aspnetidentity on mvc project getting error in var line (the entity type identityrole not part of model current context.):
using system; using system.linq; using system.web.mvc; using ez.models; using microsoft.aspnet.identity.entityframework; namespace ez.controllers { public class rolecontroller : controller { applicationdbcontext context; public rolecontroller() { context = new applicationdbcontext(); } /// <summary> /// roles /// </summary> /// <returns></returns> public actionresult index() { var roles = context.roles.tolist(); return view(roles); } /// <summary> /// create new role /// </summary> /// <returns></returns> // get: /roles/create public actionresult create() { return view(); } // // post: /roles/create [httppost] public actionresult create(formcollection collection) { try { context.roles.add(new microsoft.aspnet.identity.entityframework.identityrole() { name = collection["rolename"] }); context.savechanges(); viewbag.resultmessage = "role created !"; return redirecttoaction("index"); } catch { return view(); } } /// <summary> /// set role users /// </summary> /// <returns></returns> public actionresult setroletouser() { var list = context.roles.orderby(role => role.name).tolist().select(role => new selectlistitem { value = role.name.tostring(), text = role.name }).tolist(); viewbag.roles = list; return view(); } [httppost] [validateantiforgerytoken] public actionresult useraddtorole(string uname, string rolename) { applicationuser user = context.users.where(usr => usr.username.equals(uname, stringcomparison.currentcultureignorecase)).firstordefault(); // display roles in dropdown var list = context.roles.orderby(role => role.name).tolist().select(role => new selectlistitem { value = role.name.tostring(), text = role.name }).tolist(); viewbag.roles = list; if (user != null) { var account = new accountcontroller(); account.usermanager.addtoroleasync(user.id, rolename); viewbag.resultmessage = "role created !"; return view("setroletouser"); } else { viewbag.errormessage = "sorry user not available"; return view("setroletouser"); } } }
}
i have scripted tables in db.
this exact same code in role-security-mvc5-master project codeproject.com. difference moved tables in db ana di changed connection string. piece missing?
in identitymodel.cs have:
public class applicationdbcontext : identitydbcontext<applicationuser> { public applicationdbcontext() : base("defaultconnection", throwifv1schema: false) { } public static applicationdbcontext create() { return new applicationdbcontext(); } }
if need more code, please let me know , post.
when project created, identity, database @ locadb. when moved tables in db overwrote entire default connection string 1 created e. big probelm here default connection needs providername="system.data.sqlclient" /> , not providername="system.data.entityclient". careful there. credit needs go excellent article daniel eagle: http://danieleagle.com/blog/2014/05/setting-up-asp-net-identity-framework-2-0-with-database-first-vs2013-update-2-spa-template/. tons of detail on how use identity db first.
Comments
Post a Comment