1.代替默认的名称IdentityUser
/// <summary> /// Web用户。 /// </summary> public class WebUser : IdentityUser<Guid> { /// <summary> /// 补充昵称。 /// </summary> public string NickName { get; set; } }
2.重写上下文类即可
public class ApplicationDbContext : IdentityDbContext<WebUser, ApplicationRole, Guid> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } /// <summary> /// 重写。 /// </summary> /// <param name="builder"></param> protected override void OnModelCreating(ModelBuilder builder) { var maxKeyLength = 256; builder.Entity<WebUser>(b => { // Primary key b.HasKey(u => u.Id); // Indexes for "normalized" username and email, to allow efficient lookups b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(); b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex"); // Maps to the AspNetUsers table b.ToTable(nameof(WebUser)); // A concurrency token for use with the optimistic concurrency checking b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken(); // Limit the size of columns to use efficient database types b.Property(u => u.UserName).HasMaxLength(256); b.Property(u => u.NormalizedUserName).HasMaxLength(256); b.Property(u => u.Email).HasMaxLength(256); b.Property(u => u.NormalizedEmail).HasMaxLength(256); // The relationships between User and other entity types // Note that these relationships are configured with no navigation properties // Each User can have many UserClaims b.HasMany<IdentityUserClaim<Guid>>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired(); // Each User can have many UserLogins b.HasMany<IdentityUserLogin<Guid>>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired(); // Each User can have many UserTokens b.HasMany<IdentityUserToken<Guid>>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired(); // Each User can have many entries in the UserRole join table b.HasMany<IdentityUserRole<Guid>>().WithOne().HasForeignKey(ur => ur.UserId).IsRequired(); }); builder.Entity<IdentityUserClaim<Guid>>(b => { // Primary key b.HasKey(uc => uc.Id); // Maps to the AspNetUserClaims table b.ToTable("AspNetUserClaims"); }); builder.Entity<IdentityUserLogin<Guid>>(b => { // Composite primary key consisting of the LoginProvider and the key to use // with that provider b.HasKey(l => new { l.LoginProvider, l.ProviderKey }); // Limit the size of the composite key columns due to common DB restrictions b.Property(l => l.LoginProvider).HasMaxLength(128); b.Property(l => l.ProviderKey).HasMaxLength(128); // Maps to the AspNetUserLogins table b.ToTable("AspNetUserLogins"); }); builder.Entity<IdentityUserToken<Guid>>(b => { // Composite primary key consisting of the UserId, LoginProvider and Name b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name }); // Limit the size of the composite key columns due to common DB restrictions b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength); b.Property(t => t.Name).HasMaxLength(maxKeyLength); // Maps to the AspNetUserTokens table b.ToTable("AspNetUserTokens"); }); builder.Entity<ApplicationRole>(b => { // Primary key b.HasKey(r => r.Id); // Index for "normalized" role name to allow efficient lookups b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique(); // Maps to the AspNetRoles table b.ToTable("AspNetRoles"); // A concurrency token for use with the optimistic concurrency checking b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken(); // Limit the size of columns to use efficient database types b.Property(u => u.Name).HasMaxLength(256); b.Property(u => u.NormalizedName).HasMaxLength(256); // The relationships between Role and other entity types // Note that these relationships are configured with no navigation properties // Each Role can have many entries in the UserRole join table b.HasMany<IdentityUserRole<Guid>>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired(); // Each Role can have many associated RoleClaims b.HasMany<IdentityRoleClaim<Guid>>().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired(); }); builder.Entity<IdentityRoleClaim<Guid>>(b => { // Primary key b.HasKey(rc => rc.Id); // Maps to the AspNetRoleClaims table b.ToTable("AspNetRoleClaims"); }); builder.Entity<IdentityUserRole<Guid>>(b => { // Primary key b.HasKey(r => new { r.UserId, r.RoleId }); // Maps to the AspNetUserRoles table b.ToTable("AspNetUserRoles"); }); } }
其实官方文档都有写。
原文地址:https://www.cnblogs.com/yeqifeng2288/p/11444210.html
时间: 2024-11-06 09:45:56