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

使用独立用户账户迁移报错:IdentityUserLogin无密钥定义

Fixing "EntityType 'IdentityUserLogin' has no key defined" Error in Identity Migrations

Hey there! Let's work through this frustrating Identity entity key issue you're hitting. I've helped a bunch of folks troubleshoot this exact problem, so let's break down the likely fixes step by step.

First, Double-Check Your OnModelCreating Implementation

The most common culprit here is forgetting to call the base class's OnModelCreating method in your custom IdentityDb context. The base IdentityDbContext is responsible for configuring all the built-in Identity entities (like IdentityUserLogin, IdentityUserRole, etc.) with their correct keys and relationships. If you skip this call, EF has no idea how to map those entities.

Here's what your IdentityDb class should look like (adjust for EF6 or EF Core as needed):

public class IdentityDb : IdentityDbContext<ApplicationUser>
{
    // For EF6, this constructor is fine; for EF Core, use the DbContextOptions overload instead
    public IdentityDb() : base("DefaultConnection") { }

    // Add your custom entity DbSets here
    public DbSet<YourCustomEntity> YourCustomEntities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // THIS LINE IS NON-NEGOTIABLE - call base first!
        base.OnModelCreating(modelBuilder);

        // Now add your own entity configurations below
        // Example: modelBuilder.Entity<YourCustomEntity>().ToTable("CustomEntities");
    }

    // For EF Core, replace the default constructor with this:
    // public IdentityDb(DbContextOptions<IdentityDb> options) : base(options) { }
}

Verify Your ApplicationUser Class

Make sure your custom user class properly inherits from IdentityUser—this ties it to the Identity system's built-in schema:

public class ApplicationUser : IdentityUser
{
    // Add your custom user properties here (e.g., FullName, DateOfBirth)
    public string FullName { get; set; }
}

Ensure You're Migrating the Correct Context

If your project has multiple DbContext classes, you might be accidentally running migrations against the wrong one. Always specify your IdentityDb when creating migrations:

Package Manager Console (EF6/EF Core):

Add-Migration YourMigrationName -Context IdentityDb

.NET CLI (EF Core):

dotnet ef migrations add YourMigrationName --context IdentityDb

Check Context Registration (Owin/Startup)

For EF6 with OWIN, make sure your IdentityDb is registered correctly in your Startup or Global.asax file:

public void Configuration(IAppBuilder app)
{
    // Create a new instance of your IdentityDb per OWIN request
    app.CreatePerOwinContext(IdentityDb.Create);

    // Rest of your Identity configuration (e.g., app.UseCookieAuthentication(...))
}

Final Troubleshooting Step

If you've tried all the above and still see the error, delete any existing migrations (in the Migrations folder) and the database, then re-run the initial migration for your IdentityDb first before adding migrations for your custom entities. This ensures the Identity schema is set up correctly from the start.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:38:08