Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

relationships-ef

Relationships in EF Core

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Relationships in EF Core: the syntax, APIs, and habits you need before advancing in ASP.NET.

EF Core migrations and tracking behavior cause production incidents—LINQ translation limits matter in reviews.

You will apply Relationships in EF Core in contexts like: Line-of-business CRUD, reporting databases, and multi-tenant SaaS on SQL Server or PostgreSQL.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

EF Core models relationships—one-to-many, many-to-many, one-to-one—with navigation properties and foreign keys, similar to Django ForeignKey and ManyToManyField.

One-to-many

public class Order {
    public int Id { get; set; }
    public List<OrderLine> Lines { get; set; } = new();
}
public class OrderLine {
    public int Id { get; set; }
    public int OrderId { get; set; }
    public Order Order { get; set; } = null!;
}

Eager loading

var order = await _db.Orders
    .Include(o => o.Lines)
    .FirstAsync(o => o.Id == id);

Fluent API

Configure delete behaviors, composite keys, and indexes in OnModelCreating when conventions aren't enough.

Important interview questions and answers

  1. Q: Include vs projection?
    A: Include loads full related entities; Select projects only needed fields—often faster for APIs.
  2. Q: Cascade delete?
    A: Deleting parent may delete children—configure intentionally to avoid accidental data loss.

Self-check

  1. What property holds the FK on the many side?
  2. How do you load Order with Lines in one query?

Tip: N+1 queries happen when you loop entities and lazy-load navigation properties—use Include or project with Select in one query.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Cascade delete danger?
  • Many-to-many EF8?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump