You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从EF6迁移至EF Core时如何指定Identity用户对应的数据表?

Fixing Identity User Table Name Issue When Migrating EF6 Code First to EF Core

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 ApplicationUsers table matches your ApplicationUser class (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 just IdentityDbContext. The generic version tells EF Core to use your custom user class instead of the default IdentityUser.

Why Your Previous Attempts Failed

  • First attempt: Either the [Table] attribute was using the wrong namespace, or your OnModelCreating order was overriding it.
  • Second attempt: You added your config before calling base.OnModelCreating, so Identity’s default AspNetUsers mapping overwrote your ApplicationUsers setting.
  • Third attempt: Configuring the base IdentityUser class doesn’t work for derived entities—EF Core only cares about the concrete ApplicationUser class you’re using.

内容的提问来源于stack exchange,提问作者APW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:07:50