简体   繁体   中英

Customize IdentityUserRole in ASP.NET Core

In my asp dotnet core application, I'm changing the default IdentityUser and the IdentityRole into my own classes MesUser and MesRole

public class MesUser : IdentityUser<int>, IUser
{
    #region Property

    /// <summary>
    /// Osobní číslo
    /// </summary>
    [Column("ID")]
    public override int Id { get; set; }

    /// <summary>
    /// Křestní jméno
    /// </summary>
    [Column("FIRST_NAME")]
    public string FirstName { get; set; }

and set it in dbcontext

public DbSet<MesUser> MesUsers { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<MesUser>().ToTable("COR_USERS").HasKey(t => t.Id);

but if I do something similar with any other Identity class I get error message:

A key cannot be configured on 'MesRoleUser' because it is a derived type. The key must be configured on the root type 'IdentityUserRole'. If you did not intend for 'IdentityUserRole' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.

I know that just a link is not the rule but by following this you will learn:

  • how to change user key type,
    • in particular you do not need to override members
  • that changing the key type for user in identity involves changes in many classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM