Tutorials Entity Framework Core Tutorial

Complex Relationships in EF Core

Complex 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 50 of 100

Complex Relationships in EF Core

Beginner ✓Intermediate ✓AdvancedProfessional

Advanced · 3 — Production skills · ~6 min · Module 5: Relationships

What is this?

Complex relationships combine multiple patterns — optional FKs, many-to-many with payload, owned types, table splitting, and inheritance — in one bounded context.

Why should you care?

ShopNest orders link Customer, shipping address snapshot, payments, and items — real models rarely stop at simple one-to-many.

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 Order
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public Customer Customer { get; set; } = null!;
    public Address ShipTo { get; set; } = new(); // owned type
    public ICollection<OrderItem> Items { get; set; } = new();
    public ICollection<Payment> Payments { get; set; } = new();
}

modelBuilder.Entity<Order>().OwnsOne(o => o.ShipTo);
modelBuilder.Entity<OrderItem>()
    .HasOne(i => i.Product).WithMany().HasForeignKey(i => i.ProductId);

What happened?

  • OwnsOne embeds ShipTo columns on Orders table.
  • Order links Customer (many-to-one), Items (one-to-many), Payments (one-to-many) simultaneously.

Practice next

  1. Draw ER diagram before Fluent API.
  2. Configure each relationship explicitly in complex areas.
  3. Split bounded contexts if graph becomes unwieldy.
  4. Add Payment many-to-one Order with optional partial payments.
  5. Map inheritance hierarchy for Product types — TPH vs TPT.

Remember

Real domains mix relationship types. Owned types embed value objects. Explicit Fluent config prevents convention surprises.

ShopNest checkout model

Order aggregates owned ShipTo address, line items, and payment attempts.

Outcome: Single SaveChanges persists coherent checkout graph matching business document.

Interview prep for this lesson

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

Mid PDF Detailed
What are scalar properties, complex properties, and shadow properties?
Short answer: Scalar: Simple types like int, string, DateTime Complex: Nested objects mapped to multiple columns (Value objects) Shadow: Properties not defined in the .NET class but tracked by EF Core Real-world example…
Mid PDF Detailed
What are the types of relationships in relational databases?
Short answer: EF Core supports the standard types of relationships: One-to-One (1:1) – One entity has one related entity. One-to-Many (1:N) – One entity relates to many others. Many-to-Many (M:N) – Many entities relate t…
Mid PDF Detailed
How to handle required vs optional relationships?
Short answer: In Fluent API: Required: .HasRequired(p =&gt; p.Blog) Optional: .HasOne(p =&gt; p.Blog) .WithMany() .IsRequired(false); EF infers: Reference type (e.g., Blog) → optional by default Non-nullable value type F…
Junior PDF Detailed
What is Entity Framework Core?
Short answer: Entity Framework Core (EF Core) is a modern, lightweight, cross-platform, open-source ORM (Object-Relational Mapper) for .NET. It allows developers to interact with databases using C# objects rather than SQ…
Mid PDF Detailed
What are the main differences between Entity Framework (EF6) and EF Core?
Short answer: Feature EF6 EF Core Platform .NET Framework only Cross-platform (.NET Core) Performance Slower Faster and more optimized LINQ support Limited Enhanced Change tracking Basic More efficient Migrations Availab…
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