如何将Microsoft.AspNetCore.Identity默认表的Id类型设置为int(仅自定义了User类)
Got it, let's walk through how to switch all your ASP.NET Core Identity table IDs to int instead of the default string type. Here's exactly what you need to adjust in your code:
1. Update Your Custom User Class
Instead of inheriting the non-generic IdentityUser (which uses string as the primary key type), inherit IdentityUser<int> to explicitly set int as the ID type:
public class User : IdentityUser<int> { public string FirstName { get; set; } public string LastName { get; set; } }
2. Adjust Your DbContext to Use int Key Types
Your DBAccessContext needs to inherit the generic IdentityDbContext with explicit type parameters for your User, Role, and key type. Also, fix the typo where you mapped IdentityUserRole<string> to the "Roles" table—this should be "UserRoles" to avoid conflicts with the actual roles table:
public class DBAccessContext : IdentityDbContext<User, IdentityRole<int>, int> { public DBAccessContext(DbContextOptions<DBAccessContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // Customize table names and override defaults builder.Entity<User>(entity => { entity.ToTable(name: "Users");}); builder.Entity<IdentityRole<int>>(entity => { entity.ToTable(name: "Roles"); }); builder.Entity<IdentityUserRole<int>>(entity => { entity.ToTable("UserRoles"); }); // Fixed table name builder.Entity<IdentityUserClaim<int>>(entity => { entity.ToTable("Claims"); }); builder.Entity<IdentityUserLogin<int>>(entity => { entity.ToTable("Logins"); }); builder.Entity<IdentityRoleClaim<int>>(entity => { entity.ToTable("RoleClaims"); }); builder.Entity<IdentityUserToken<int>>(entity => { entity.ToTable("UserTokens"); }); // Apply role seeding configuration builder.ApplyConfiguration(new RoleConfiguration()); } }
3. Update Identity Service Configuration
In your ConfigureIdentity method, specify the IdentityRole<int> type when building the identity services—this tells the framework to use int as the key type across all Identity components:
public static void ConfigureIdentity(this IServiceCollection services) { var builder = services.AddIdentityCore<User>(q => { q.User.RequireUniqueEmail = true; }); // Explicitly set Role type and key type (int) builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole<int>), services); builder.AddEntityFrameworkStores<DBAccessContext>() .AddDefaultTokenProviders() .AddRoleValidator<RoleValidator<IdentityRole<int>>>() // Optional but ensures valid role validation .AddRoleManager<RoleManager<IdentityRole<int>>>(); // Add if you need role management functionality }
4. Fix Role Seeding Configuration (If Applicable)
If your RoleConfiguration class seeds default roles, make sure it targets IdentityRole<int> and uses int IDs:
public class RoleConfiguration : IEntityTypeConfiguration<IdentityRole<int>> { public void Configure(EntityTypeBuilder<IdentityRole<int>> builder) { builder.HasData( new IdentityRole<int> { Id = 1, Name = "Admin", NormalizedName = "ADMIN" }, new IdentityRole<int> { Id = 2, Name = "User", NormalizedName = "USER" } ); } }
After making these changes, all your Identity tables will use int as their primary key type instead of the default string. Don't forget to create a new migration and update your database to apply these schema changes!
内容的提问来源于stack exchange,提问作者Nour Mawla

