0Pricing
C# Academy · Lesson

Relationships: One-to-Many & Many-to-Many

Model and query one-to-many and many-to-many relationships with navigation properties and Fluent API.

Relationships in EF Core

EF Core models three types of relationships between entities: one-to-one, one-to-many, and many-to-many. Each is expressed with navigation properties and optionally configured with the Fluent API.

One-to-Many: Entity Setup

A Blog has many Posts. The dependent (Post) holds the foreign key. Navigation properties on both sides allow traversal in either direction.

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public ICollection<Post> Posts { get; set; } = new List<Post>();
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; } = "";
    public int BlogId { get; set; }        // FK
    public Blog Blog { get; set; } = null!; // nav property
}

All lessons in this course

  1. DbContext & DbSet Basics
  2. Migrations & Schema Management
  3. CRUD Operations with EF Core
  4. Relationships: One-to-Many & Many-to-Many
← Back to C# Academy