Tutorials Entity Framework Core Tutorial
One-to-One Relationships in EF Core
One-to-One Relationships 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 41 of 100
One-to-One Relationships in EF Core
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~6 min · Module 5: Relationships
What is this?
One-to-one means one entity row pairs with exactly one related row — Customer and CustomerProfile, Product and ProductDetail.
Why should you care?
ShopNest stores bulky SEO metadata separately from Product core columns — one-to-one keeps hot catalog queries lean.
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 ProductDetail
{
public int ProductId { get; set; } // PK + FK
public Product Product { get; set; } = null!;
public string LongDescription { get; set; } = "";
public string MetaKeywords { get; set; } = "";
}
modelBuilder.Entity<ProductDetail>()
.HasOne(d => d.Product)
.WithOne(p => p.Detail)
.HasForeignKey<ProductDetail>(d => d.ProductId);
What happened?
- ProductId serves as both PK and FK to Product.
- WithOne/HasForeignKey configures shared-primary-key one-to-one — common for extension tables.
Practice next
- Add ProductDetail with ProductId as PK/FK.
- Configure WithOne in Fluent API and migrate.
- Query with Include(p => p.Detail) only when detail page needed.
- Make one-to-one optional — nullable ProductId vs required detail.
- Query split: load Product first, Detail second with explicit load.
Remember
One-to-one uses unique FK or shared PK. Split heavy columns into extension entity. Load detail navigation only when needed.
ShopNest product detail tab
Listing API skips ProductDetail; detail page Include Detail for long description.
Outcome: Fast category scroll; rich SEO fields only on product page.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!