locked
IdentityRole is not part of the model for the current context when extending IdentityRole RRS feed

  • 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 { getset; }
            public AspNetApplications Application { getset; }
        }

    I have also updated my RoleManager in my IdentityConfig file like this:

        public class ApplicationRoleManager : RoleManager<ApplicationRole>
        {
            public ApplicationRoleManager(
                IRoleStore<ApplicationRolestring> 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(thisDefaultAuthenticationTypes.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 :

    https://stackoverflow.com/questions/51985514/the-entity-type-identityrole-is-not-part-of-the-model-for-the-current-context-af/52029846#52029846 

    1. Create an ASP.NET Web Application
    2. Make sure you select MVC and the Authentication is Individual User Accounts
    3. Go to Models folder → Open IdentityModels.cs and Create ApplicationRole class:

      public class ApplicationRole : IdentityRole
      {
          public string ApplicationId { get; set; }
      }
    4. Change GenerateUserIdentityAsync method of ApplicationUser to accept parameter of type of UserManager<ApplicationUser, string>:

      public class ApplicationUser : IdentityUser
      {
          public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager)
          {
    5. Change ApplicationDbContext base class and introduce all the generic parameters:

      public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
      {
          public ApplicationDbContext()
              : base("DefaultConnection")
          {
    6. Build the project.

    7. Go to TOOLS menu → Nuget Package Manager → click Package Manager Console
    8. Type Enable-Migrations and press <kbd>Enter</kbd> and wait until the task get completed.
    9. Type Add-Migration "ApplicationRole" and press <kbd>Enter</kbd> and wait until the task get completed.
    10. Type Update-Database and press <kbd>Enter</kbd> and wait until the task get completed.
    11. Go to App_Start folder → Open IdentityConfig.cs and Change the ApplicationUserManagerclass to derive from UserManager<ApplicationUser, string> and also change its Createmethod to return a UserManage aware of ApplicationRole:

      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>()));
    12. 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>()));
          }
      }
    13. 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