The entity type IdentityRole is not part of the model for the current context after extending Roles table

The entity type IdentityRole is not part of the model for the current context after extending Roles table



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?





Not sure if you're using Code First or Database First, but customizing roles so far requires modification of UserStore and/or UserManager (see here and here for further explanation).
– Tetsuya Yamamoto
Aug 23 at 12:46


UserStore


UserManager





Hi @TetsuyaYamamoto, I'm using Code First. I have updated my question with some changes I made. Can you check if I'm missing something?
– JianYA
Aug 23 at 12:50





@TetsuyaYamamoto I tried to follow your examples but no change.
– JianYA
Aug 23 at 13:21




1 Answer
1



Short answer



The main problem in above code is in Create method of UserManager. In that method, you should create a UserManager using a UserStore which is aware of the new role class which you created. To do so, you can use an instance of ApplicationUserStore class which you have or create a new user store this way:


Create


UserManager


UserManager


UserStore


ApplicationUserStore


new UserStore<ApplicationUser, [YourRoleClass], string,
IdentityUserLogin, IdentityUserRole, IdentityUserClaim(
context.Get<ApplicationDbContext>())



To add a new property to IdentityRole, you can follow the following steps:


IdentityRole



Go to Models folder → Open IdentityModels.cs and Create ApplicationRole class containing the custom property that you want to add:


public class ApplicationRole : IdentityRole //My custom role class

public string ApplicationId get; set; //My custom property



Change GenerateUserIdentityAsync method of ApplicationUser to accept parameter of type of UserManager<ApplicationUser, string>:


GenerateUserIdentityAsync


ApplicationUser


UserManager<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:


ApplicationDbContext


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



Build the project.


Enable-Migrations


Add-Migration "ApplicationRole"


Update-Database



Go to App_Start folder → Open IdentityConfig.cs and Change the ApplicationUserManager class to derive from UserManager<ApplicationUser, string> and also change its Create method to return a UserManage aware of ApplicationRole:


ApplicationUserManager


UserManager<ApplicationUser, string>


Create


UserManage


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>()));



To manage roles, create ApplicationRoleManager class in the same file:


ApplicationRoleManager


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:


ConfigureAuth


ConfigureAuthapp.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);



Now the project is ready to take advantage of the new ApplicationRole.


ApplicationRole





I'll try this. Thank you so much. At the moment I just inherited the IdentityRole with a discriminator field in my table. Seems to work fine as well? What is the difference between this method and mine?
– JianYA
Aug 27 at 4:51





You're welcome, The main difference is in step 11 (and 4).
– Reza Aghaei
Aug 27 at 5:32







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ