更改.net core 3中默认的身份认证生成的数据库名称。

Posted yeqifeng2288

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改.net core 3中默认的身份认证生成的数据库名称。相关的知识,希望对你有一定的参考价值。

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

其实官方文档都有写。

以上是关于更改.net core 3中默认的身份认证生成的数据库名称。的主要内容,如果未能解决你的问题,请参考以下文章

.net core 生成的身份认证的代码在哪?还有生成的注册和登录页面在哪?

ASP.NET Core JWT 身份验证更改声明(子)

手把手教你 .NET Core 3.1 JWT身份认证

无法更改 Asp 身份表名称.....Asp .Net Core 2?

ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 14. ASP.NET Core Identity 入门

更改 ASP.NET Core 中 DateTime 解析的默认格式