Home >Backend Development >C++ >How to Customize ASP.NET Identity Table Names?
Customizing ASP.NET Identity Table Names
ASP.NET Identity uses default table names prefixed with "AspNet." This article shows how to customize these names to fit your application's naming conventions.
Method 1: Extending the Identity Model
To alter table names, extend the IdentityModel.cs
file and override the OnModelCreating
method within your DbContext
. Use EntityTypeConfiguration<T>
to define custom table names.
<code class="language-csharp">protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>().ToTable("MyUsers"); modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers"); // If ApplicationUser extends IdentityUser }</code>
Method 2: Directly Modifying the DbContext
Alternatively, directly modify your DbContext
class:
<code class="language-csharp">public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>().ToTable("MyUsers"); modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers"); modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles"); modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins"); modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims"); modelBuilder.Entity<IdentityRole>().ToTable("MyRoles"); } }</code>
Both methods remove the "AspNet" prefix, allowing you to use your preferred table names. Choose the method that best suits your project structure.
The above is the detailed content of How to Customize ASP.NET Identity Table Names?. For more information, please follow other related articles on the PHP Chinese website!