Asked by:
IdentityRole is not part of the model for the current context when extending IdentityRole

Question
-
User-472004370 posted
Hello,
I have just extended my IdentityRole following this tutorial: http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/
public class ApplicationRole : IdentityRole { public ApplicationRole() : base() { } public ApplicationRole(string name) : base(name) { } public String ApplicationId { get; set; } public AspNetApplications Application { get; set; } }
I have also updated my RoleManager in my IdentityConfig file like this:
public class ApplicationRoleManager : RoleManager<ApplicationRole> { public ApplicationRoleManager( IRoleStore<ApplicationRole, string> roleStore) : base(roleStore) { } public static ApplicationRoleManager Create( IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) { return new ApplicationRoleManager( new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>())); } }
However, the issue starts when I try to Seed my data, I get this error: IdentityRole is not part of the model for the current context
Also, when I try to login to my application, it shows that this line is giving out the error:
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
I'm quite confused as to what I need to do properly to add one column to my AspNetRoles table and actually get data from it. E.g.
List<ApplicationRole> data = (from ar in dbContext.Roles join a in dbContext.AspNetApplications on ar.ApplicationId equals a.Id select new ApplicationRole { Id = ar.Id, ApplicationId = ar.ApplicationId, Name = ar.Name }).ToList();
I'm using ASP.NET MVC5. Hope someone can help with this. I've tried this for several days now.
Sunday, August 26, 2018 4:10 AM
All replies
-
User1724605321 posted
Hi Amos Ang ,
You can check the detail answer and step by step extending the Roles :
- Create an ASP.NET Web Application
- Make sure you select MVC and the Authentication is Individual User Accounts
-
Go to Models folder → Open IdentityModels.cs and Create ApplicationRole class:
public class ApplicationRole : IdentityRole { public string ApplicationId { get; set; } }
-
Change
GenerateUserIdentityAsync
method ofApplicationUser
to accept parameter of type ofUserManager<ApplicationUser, string>
:public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager) {
-
Change
ApplicationDbContext
base class and introduce all the generic parameters:public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> { public ApplicationDbContext() : base("DefaultConnection") {
-
Build the project.
- Go to TOOLS menu → Nuget Package Manager → click Package Manager Console
- Type
Enable-Migrations
and press <kbd>Enter</kbd> and wait until the task get completed. - Type
Add-Migration "ApplicationRole"
and press <kbd>Enter</kbd> and wait until the task get completed. - Type
Update-Database
and press <kbd>Enter</kbd> and wait until the task get completed. -
Go to App_Start folder → Open IdentityConfig.cs and Change the
ApplicationUserManager
class to derive fromUserManager<ApplicationUser, string>
and also change itsCreate
method to return aUserManage
aware ofApplicationRole
:public class ApplicationUserManager : UserManager<ApplicationUser, string> { public ApplicationUserManager(IUserStore<ApplicationUser, string> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>(context.Get<ApplicationDbContext>()));
-
To manage roles, create
ApplicationRoleManager
class in the same file:public class ApplicationRoleManager : RoleManager<ApplicationRole> { public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store) { } public static ApplicationRoleManager Create( IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) { return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>())); } }
-
Go to App_Start folder → Open Startup.Auth.cs and add the following code to the
ConfigureAuth
method:app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
Then test the roles :
var roleManager = new ApplicationRoleManager(new Microsoft.AspNet.Identity.EntityFramework.RoleStore<ApplicationRole>(new ApplicationDbContext())); if (!roleManager.RoleExists("role1")) { var role = new ApplicationRole(); role.Name = "role1"; role.ApplicationId = "app1"; roleManager.Create(role); } var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = userManager.FindByEmail("v-nany@microsoft.com"); var result = userManager.AddToRole(user.Id, "role1");
Best Regards,
Nan Yu
Monday, August 27, 2018 9:38 AM