What is a keyless entity type in EF Core?

Entity Framework & EF Core Modelling Intermediate 2–3 Yrs3–5 Yrs Conceptual 1 min read

Quick Answer

A keyless entity type has no primary key, so EF never tracks it and it's read-only. Use it to map a database view, the result of a raw SQL query, or a stored-procedure result set into a typed shape. Configure with [Keyless] or HasNoKey().

A keyless entity type is one you tell EF has no primary key with HasNoKey. Because there's no key, EF can't track it, so it's strictly read-only. I use it to give a typed shape to things that aren't really tables — a database view, the output of a raw SQL query, or a stored procedure's result set. So I can define an OrderSummary class, map it to a view, and query it with LINQ, but I can't insert or update through it.

  • modelBuilder.Entity<OrderSummary>().HasNoKey().ToView("vw_OrderSummary"); then query db.OrderSummaries.Where(...).
  • db.Database.SqlQuery<ReportRow>($"EXEC dbo.usp_Report {@from}") — maps a proc result to a keyless type.
  • No Add/Update/Delete, no change tracking, no identity resolution.
  • Good for reporting DTOs and read models without pretending they're tables.