我如何将 UserManager 与自定义用户一起使用?

How can i use UserManager with custom user?

我创建了继承 IdentityUser<int>.

的自定义用户 class
[Table("Users", Schema = "UserData")]
public class User : IdentityUser<int>
{
    /// <summary>
    /// Property for sake of creating One-to-One relationship UserDetails -> User
    /// </summary>
    [Required]
    public UserDetails UserDetails { get; set; }
}

public class BlogDbContext : IdentityDbContext<User, IdentityRole<int>, int>
    {
        public BlogDbContext(DbContextOptions<BlogDbContext> options)
                   : base(options) {}

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

            //UserDataConfig
            builder.ApplyConfiguration(new UserConfiguration());
            builder.ApplyConfiguration(new UserDetailsConfiguration());


            //UserData

            builder.Entity<Location>()
                .HasOne<UserDetails>(s => s.UserDetails)
                .WithOne(g => g.Location)
                .HasForeignKey<UserDetails>(ad => ad.LocationId);

            builder.Entity<User>()
                .HasOne<UserDetails>(s => s.UserDetails)
                .WithOne(g => g.User)
                .HasForeignKey<UserDetails>(ad => ad.UserId);
   
            base.OnModelCreating(builder);
        }

        //UserData
        public DbSet<User> Users { get; set; }
        public DbSet<UserDetails> UserDetails { get; set; }
    }
}

现在我正在尝试创建以下字段

private UserManager<User, int> _userManager;

而我的错误:

The type 'ApplicationCore.DataModel.UserData.User' cannot be used as type parameter 'TUser' in the generic type or method 'UserManager<TUser, TKey>

我该如何解决?感谢您的关注。

将用户替换为 IdentityUser,将角色替换为 IdentityRole,工作正常。喜欢

public class DataContext : IdentityDbContext<IdentityUser,IdentityRole, string, IdentityUserClaim<string>, IdentityUserRole<string>,
       IdentityUserLogin<string>, IdentityRoleClaim<string>,IdentityUserToken<string>>

或使用:-

public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}
public class BlogDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}

它将解决您的问题。