Search code examples
c#mysqlasp.net-coreasp.net-identity

How to create custom UserClaims table in MySQL using ASP.NET Core with EF and Identity?


I am trying to implement ASP.NET Core Identity in my MySQL database. I want to save claims in table named different that "aspnetuserclaims". I managed to point custom tables for users using this in my OnModelCreating() method in MyContext which inherits from IdentityDbContext class:

 modelBuilder.Entity<ApplicationUser>().ToTable("users").Property(p => p.Id).HasColumnName("Id");

But, when I want to do the same with my class which inherits form IdentityUserClaim using:

modelBuilder.Entity<ApplicationUserClaim>().ToTable("userclaims").Property(p => p.Id).HasColumnName("Id");

It doesn't work at all.

Both ApplicationUser and ApplicationUserClaim are clean class looking like this:

  public class ApplicationUser : IdentityUser<string> {}

Exception:

MySqlException: Table 'dbname.aspnetuserclaims' doesn't exist


Solution

  • IdentityUser has generic type overloads. You're using IdentityUser<TKey>, but if you want to customize something like the claim class, you need to specify IdentityUser<TKey,TUserClaim,TUserRole,TUserLogin>.

    Additionally, IdentityDbContext also has generic type overloads, so likewise, you need to ensure you're using the correct overload there, namely: IdentityDbContext<TUser,TRole,TKey,TUserClaim,TUserRole,TUserLogin,TRoleClaim,TUserToken>.