从EF6迁移至EF Core时如何指定Identity用户对应的数据表?
Hey there, I’ve run into this exact problem a dozen times when helping teams migrate from EF6 to EF Core with Identity—let’s break down what’s going wrong and how to fix it.
Your core issue is the order of calling base.OnModelCreating(modelBuilder) in your DbContext. Identity’s default table mappings (like forcing AspNetUsers) are set up inside that base method. If you add your custom table name config before calling base, your settings get overwritten completely. That’s why your second attempt didn’t work.
The Correct Steps to Configure Your Table Name
1. Fix the OnModelCreating Order
Move the base call to the top first, then apply your custom config. This way, your settings override Identity’s defaults instead of the other way around:
protected override void OnModelCreating(ModelBuilder modelBuilder) { // Let Identity do its default setup first base.OnModelCreating(modelBuilder); // Now override the ApplicationUser table name modelBuilder.Entity<ApplicationUser>(b => { b.ToTable("ApplicationUsers"); // If you need to tweak other properties (like column names), do it here too: // b.Property(u => u.Email).HasColumnName("UserEmail"); // b.HasKey(u => u.Id); // Only needed if you changed the primary key type }); }
2. Double-Check the [Table] Attribute
If you prefer using data annotations instead of fluent API, make sure you’re using the right namespace for the [Table] attribute. EF Core works with both System.ComponentModel.DataAnnotations.Schema (from EF6) and Microsoft.EntityFrameworkCore, but sometimes namespace conflicts can cause it to be ignored. Here’s the safe way:
using System.ComponentModel.DataAnnotations.Schema; [Table("ApplicationUsers")] public class ApplicationUser : IdentityUser { // Your custom properties go here }
Even with the attribute, you still need the correct OnModelCreating order—because the base method’s config can still override annotations if you call it after your setup.
3. Stop Trying to Configure IdentityUser Directly
Your third attempt failed because EF Core doesn’t let you configure table mappings for the base IdentityUser class when you have a derived ApplicationUser. The derived class is the actual entity EF Core uses, so you only need to configure that one.
Extra Migration Tips for Your EF6 to EF Core Move
Since you’re coming from EF6 Code First, don’t forget these details:
- Before running EF Core migrations, make sure your existing
ApplicationUserstable matches yourApplicationUserclass (column names, data types, primary key, etc.). Mismatches here will cause runtime errors even if the table name is correct. - If you already generated bad migration files (that reference
AspNetUsers), delete those files first. Then re-generate and apply migrations:dotnet ef migrations add MigrateFromEF6Identity dotnet ef database update - Double-check your DbContext inherits from
IdentityDbContext<ApplicationUser>, not justIdentityDbContext. The generic version tells EF Core to use your custom user class instead of the defaultIdentityUser.
Why Your Previous Attempts Failed
- First attempt: Either the
[Table]attribute was using the wrong namespace, or yourOnModelCreatingorder was overriding it. - Second attempt: You added your config before calling
base.OnModelCreating, so Identity’s defaultAspNetUsersmapping overwrote yourApplicationUserssetting. - Third attempt: Configuring the base
IdentityUserclass doesn’t work for derived entities—EF Core only cares about the concreteApplicationUserclass you’re using.
内容的提问来源于stack exchange,提问作者APW

