Tutorials Entity Framework Core Tutorial

Relationships in Code First EF Core

Relationships in Code First 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 18 of 100

Relationships in Code First EF Core

BeginnerIntermediateAdvancedProfessional

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

What is this?

Relationships connect entities through foreign key properties and navigation properties — Customer has many Orders, Order has many OrderItems, Product belongs to Category.

Why should you care?

ShopNest checkout loads related data; relationships generate FK constraints and enable Include without manual join SQL.

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 OrderItem
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public Order Order { get; set; } = null!;
    public int ProductId { get; set; }
    public Product Product { get; set; } = null!;
    public int Quantity { get; set; }
}

What happened?

  • OrderId and ProductId are FK columns.
  • Order and Product navigations let EF traverse graphs and configure cascades in Fluent API.

Practice next

  1. Wire Order ↔ Customer and OrderItem ↔ Product navigations both ways.
  2. Configure delete behavior in Fluent API for Order–OrderItem.
  3. Add migration and verify FK constraints in SSMS.
  4. Make OrderItem–Product relationship optional with nullable ProductId (if business allows).
  5. Add shadow property FK via Fluent API without C# property.

Remember

FK property + navigation defines a relationship. Configure delete behavior explicitly for production. Bidirectional navigations clarify the model.

ShopNest order graph

OrderItem rows tie each purchased Product to an Order with enforced FK integrity.

Outcome: Orphan line items cannot exist — refunds always trace to a product.

Interview prep for this lesson

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

Junior PDF Detailed
What is Code First approach?
Short answer: Advantages and when to use it Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# c…
Junior PDF Detailed
What is Code First approach?
Short answer: Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# code (POCOs) Easily version-con…
Mid PDF Detailed
How does Code First handle schema changes in the database?
Short answer: EF Core uses migrations to track and apply schema changes: You modify your C# models. Use Add-Migration to create a migration class. Use Update-Database to apply the changes to the DB. EF Core compares the…
Junior PDF Detailed
What is Database First approach?
Short answer: Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to vers…
Junior PDF Detailed
What is Database First approach?
Short answer: Use‑cases and pros and cons Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy data…
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