requires a primary key to be defined .NET Origin: Application & API Last updated 13 Oct 2026

EF Core: The entity type 'X' requires a primary key to be defined

EF Core found an entity class without a key property it can recognise.

Exact message you may see requires a primary key to be definedIf you intended to use a keyless entity type, call 'HasNoKey'
Seen on: .NET

Meaning

EF looks for Id or <TypeName>Id. Other names need [Key] or HasKey(); keyless query types need HasNoKey(). DTOs accidentally exposed as DbSet also trigger it.

Common causes

  • Key property not named Id/<Type>Id
  • Composite key not configured
  • Class meant to be keyless (view/query)
  • Non-entity class included as DbSet or navigation

⚡ Quick fix

  1. Add [Key] or modelBuilder.Entity<T>().HasKey(...)
  2. Use HasNoKey() for views
  3. Remove accidental DbSet/navigation

Detailed fix by platform

C#

  1. csharp
    modelBuilder.Entity<OrderLine>().HasKey(l => new { l.OrderId, l.LineNo });
    modelBuilder.Entity<SalesReport>().HasNoKey().ToView("vw_sales");

How to diagnose

  1. Entity — Named in the error
  2. Key — Property naming

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

A second operation was started on this context — EF Core: A second operation was started on this context instance before a previous operation completed (.NET)A second operation wa…Cannot access a disposed context instance — ObjectDisposedException: Cannot access a disposed context instance (.NET)Cannot access a dispo…cannot be tracked because another instance with the same key — The instance of entity type 'X' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked (.NET)cannot be tracked bec…DbUpdateException — DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. (.NET)DbUpdateExceptionJsonException object cycle — JsonException: A possible object cycle was detected (.NET)JsonException object …No database provider has been configured — InvalidOperationException: No database provider has been configured for this DbContext (.NET)No database provider …The LINQ expression could not be translated — EF Core: The LINQ expression 'x' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly (.NET)The LINQ expression c…Unable to create a 'DbContext' — dotnet ef: Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type DbContextOptions' was thrown while attempting to create an instance (.NET)Unable to create a 'D…requires …

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.