I have extended my AspNetRoles that was created by Entity Framework to look like this:
public class AspNetRoles:IdentityRole { public AspNetRoles() : base() { } public String Label { get; set; } public String ApplicationId { get; set; } public AspNetApplications Application { get; set; } public static readonly String SystemAdministrator = "SystemAdministrator"; } I understood that because I have extended the identityrole table, I had to make changes to my usermanager. This is what I have done:
public class ApplicationUserManager : UserManager<ApplicationUser> { public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); // Configure validation logic for usernames manager.UserValidator = new UserValidator<ApplicationUser>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; // Configure user lockout defaults manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); manager.MaxFailedAccessAttemptsBeforeLockout = 5; // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user // You can write your own provider and plug it in here. manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> { MessageFormat = "Your security code is {0}" }); manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); manager.EmailService = new EmailService(); manager.SmsService = new SmsService(); var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); } return manager; } } // Configure the application sign-in manager which is used in this application. public class ApplicationSignInManager : SignInManager<ApplicationUser, string> { public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) : base(userManager, authenticationManager) { } public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user) { return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager); } public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context) { return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication); } } public class ApplicationRoleManager : RoleManager<AspNetRoles>, IDisposable { public ApplicationRoleManager(RoleStore<AspNetRoles> store) : base(store) { } public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) { //AppIdentityDbContext db = context.Get<AppIdentityDbContext>(); //AppRoleManager manager = new AppRoleManager(new RoleStore<AppRole>(db)); return new ApplicationRoleManager(new RoleStore<AspNetRoles>(context.Get<ApplicationDbContext>())); //return manager; } } public class ApplicationUserStore<TUser> : UserStore<TUser, AspNetRoles, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUserStore<TUser>, IUserStore<TUser, string>, IDisposable where TUser : IdentityUser { public ApplicationUserStore(DbContext context) : base(context) { } } This is my DBContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, AspNetRoles, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> { public virtual DbSet<AspNetUsersExtendedDetails> AspNetUsersExtendedDetails { get; set; } public virtual DbSet<AspNetApplications> AspNetApplications { get; set; } public virtual DbSet<AspNetEventLogs> AspNetEventLogs { get; set; } public ApplicationDbContext() : base("AppStudio") { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } However, I get this error when I start my application:
The entity type IdentityRole is not part of the model for the current context.
I'm not sure why this is happening. Have I missed something that needs to be changed after extending my roles table?
UserStoreand/orUserManager(see here and here for further explanation).