Tutorials Entity Framework Core Tutorial

Composite Keys in EF Core

Composite Keys in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.

On this page

Entity Framework Core Tutorial · Lesson 19 of 100

Composite Keys in EF Core

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Foundations · ~6 min · Module 2: Code First Approach

What is this?

A composite key uses two or more properties together as the primary key. Configure it with [PrimaryKey] attribute or Fluent HasKey when a single Id column is not enough.

Why should you care?

ShopNest warehouse bin locations may be unique by WarehouseId + Aisle + Bin — composite keys model natural identifiers without surrogate Id.

See it live — copy this example

Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).

public class WarehouseBin
{
    public int WarehouseId { get; set; }
    public string Aisle { get; set; } = "";
    public string BinCode { get; set; } = "";
    public int ProductId { get; set; }
}

// Fluent API:
modelBuilder.Entity<WarehouseBin>()
    .HasKey(b => new { b.WarehouseId, b.Aisle, b.BinCode });

What happened?

  • HasKey with anonymous type defines composite PK.
  • EF creates clustered index on all three columns.
  • FindAsync requires all key parts.

Practice next

  1. Identify a natural composite key in ShopNest inventory (e.g., wishlist UserId + ProductId).
  2. Configure HasKey and add migration.
  3. Try FindAsync with all key values vs missing one.
  4. Model OrderItem with composite key OrderId + LineNumber instead of Id.
  5. Query by key: _context.WarehouseBins.Find(1, "A", "B12").

Remember

Composite keys combine multiple columns as PK. Configure via Fluent HasKey or [PrimaryKey]. Surrogate keys are often simpler for ORM apps.

ShopNest stock location key

Inventory service uses WarehouseId+Aisle+BinCode composite key to prevent duplicate bin assignments.

Outcome: Natural key matches warehouse labels on physical shelves.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
How do you configure mappings (table names, column names, keys) using Data Annotations vs Fluent API?
Short answer: 🧩 Data Annotations (in the entity class): [Table(&quot;Users&quot;)] public class User Example code { [Key] public int Id { get; set; } [Column(&quot;UserName&quot;)] public string Name { get; set; } } 🧠…
Mid PDF Detailed
What are foreign keys and primary keys?
Short answer: How to define them via annotations &amp; Fluent API? Explain a bit more Primary Key (PK): Unique identifier of a record. Foreign Key (FK): A field that references a PK in another table. Data Annotations: [K…
Junior PDF Detailed
What are foreign keys and primary keys? How to define them via
Short answer: nnotations &amp; Fluent API? Primary Key (PK): Unique identifier of a record. Foreign Key (FK): A field that references a PK in another table. Data Annotations: [Key] public int Id { get; set; } [ForeignKey…
Junior PDF Detailed
What is a composite key?
Short answer: How to define composite keys in EF Core? A composite key is a primary key made of multiple columns. EF Core does not support composite keys via data annotations, so you must use Fluent API: modelBuilder.Ent…
Junior PDF Detailed
What is a composite key?
Short answer: composite key is a primary key made of multiple columns. EF Core does not support composite keys via data annotations, so you must use Fluent PI: modelBuilder.Entity&lt;OrderDetail&gt;() .HasKey(od =&gt; ne…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Tutorial
Course syllabus

Entity Framework Core Tutorial

Module 1: EF Core Fundamentals
Module 2: Code First Approach
Module 3: CRUD Operations
Module 4: LINQ
Module 5: Relationships
Module 6: Advanced EF Core
Module 7: Performance Optimization
Module 8: Enterprise Architecture
Module 9: Testing & Debugging
Module 10: Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details