The entity type 'IdentityPasskeyData' requires a primary key to be defined. ... came
from an IdentityDbContext whose OnModelCreating override did not call
base.OnModelCreating(builder). Putting that call first in the override fixed it in the
lab; the base method is where the Identity keys appear to come from, since it is the only line added.
The entity name in the message was a surprise: the list this lab started from expected
IdentityUserLogin<string>. On Identity 10.0.10 the first entity the validator
complained about was IdentityPasskeyData. In this run the base class's configuration never ran.
The error
Server console, at startup, on the error stream; the top of the stack trace:
Unhandled exception. System.InvalidOperationException: The entity type 'IdentityPasskeyData' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model, IDiagnosticsLogger`1 logger)
...
Why it happens
The context added a table of its own and configured it in an override that replaced the base method instead of extending it:
...
class AppDb(DbContextOptions<AppDb> options) : IdentityDbContext<IdentityUser>(options)
{
public DbSet<Clinic> Clinics => Set<Clinic>();
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Clinic>().Property(c => c.Name).HasMaxLength(100);
}
}
The base call is the only line the fix adds, so the Identity keys come from the base class's
OnModelCreating; without it the model validator refused the Identity type it named. The stack trace
shows ModelValidator.ValidateNonNullPrimaryKeys, and the
Program.cs line it pointed at was the EnsureCreated() call, the first
use of the model. No user was created and the app did not start listening.
The fix
...
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Clinic>().Property(c => c.Name).HasMaxLength(100);
}
...
With the base call first the same seed printed Create user: Succeeded. The lab ran
only this order, base call first and its own configuration after it. Other Identity versions may
name a different entity in the message; only 10.0.10 was run here.
How it was reproduced
A dotnet new web -f net10.0 project on .NET SDK 11.0.100 with
Microsoft.AspNetCore.Identity.EntityFrameworkCore 10.0.10 and
Microsoft.EntityFrameworkCore.Sqlite 10.0.10, Identity registered with
AddIdentityCore<IdentityUser>().AddEntityFrameworkStores<AppDb>(), and a seed
in a scope after builder.Build() that calls EnsureCreated() and creates one
fictional user. IdnLab, port 15620, AppDb, Clinic, lab.db, the user and its password
are the test's; a real seed reads a password from configuration or user-secrets, never from
source. Two runs, each built with dotnet build and started with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620: the broken one in a
new copy of the base project, the fixed one after moving the first file aside.
Frequently asked
- Why does IdentityPasskeyData require a primary key?
- The IdentityDbContext's OnModelCreating override did not call base.OnModelCreating(builder). In the lab adding the base call as the first line fixed it, so the Identity keys appear to come from that base method.
- Where does base.OnModelCreating go in an IdentityDbContext?
- First in the override, before your own configuration. The lab ran only that order, and the seed then succeeded.
- Why does the error name IdentityPasskeyData and not IdentityUserLogin?
- In the lab with Identity 10.0.10 the validator reported IdentityPasskeyData first. Other versions were not run; the fix is the same base call.
The same message for an entity of your own is covered in The entity type ... requires a primary key to be defined. More decoded errors in the Fixes category.