One-to-Many Relationships in EF Core

A one-to-many relationship associates one principal entity with multiple dependent entities. For example, an order can contain many order items, while each order item belongs to at most one order. EF Core can discover this relationship by convention, or you can configure it explicitly using Fluent API.

🤯 Wait... Entity Framework Extensions Is FREE?!

Yes, really! Get a $0 perpetual commercial license with ALL database providers. Not a trial. No subscription. For eligible new customers under $1M revenue.

See How to Get It FREE →

Basic One-to-Many Relationship

An Order can contain many OrderItem records. The following example uses only the collection navigation on Order:

language-csharp
|
public class Order
{
    public int OrderId { get; set; }
    public ICollection<OrderItem> Items { get; set; } = new List<OrderItem>();
}

public class OrderItem
{
    public int OrderItemId { get; set; }
    public string ProductName { get; set; } = null!;
}

EF Core discovers a one-to-many relationship from Order.Items. Since OrderItem does not declare a foreign key property, EF Core creates a shadow foreign key named OrderId. The property exists in the EF Core model and as a column in the database, but not in the C# class.

With this model, the shadow foreign key is nullable by convention, so an OrderItem can exist without an associated Order. In a typical order-processing application, order items are usually required to belong to an order; we will model that more common rule in the explicit foreign key example below.

See the official EF Core one-to-many documentation for additional relationship patterns.

Configure One-to-Many with Fluent API

EF Core can discover the basic example without OnModelCreating. You can also express the mapping explicitly using the Has/With pattern, where HasMany() identifies the collection navigation and WithOne() identifies the other end of the relationship:

language-csharp
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>()
        .HasMany(o => o.Items)
        .WithOne()
        .HasForeignKey("OrderId")
        .IsRequired(false);
}

This configuration uses the same Order and OrderItem classes as the basic example. WithOne() has no argument because OrderItem has no inverse navigation property; HasForeignKey("OrderId") specifies the shadow foreign key, and IsRequired(false) makes the relationship optional.

When both entities have navigation properties, you can instead use HasMany(o => o.Items).WithOne(i => i.Order). The equivalent configuration can start from OrderItem with HasOne(i => i.Order).WithMany(o => o.Items); configure the relationship once, not from both ends separately.

For required relationships, use IsRequired() or a non-nullable foreign key as in the example below. Use OnDelete() when you need to customize deletion behavior; see Cascade Delete in EF Core for the available behaviors and their implications.

Inverse Navigation Property

You can add an inverse reference navigation property to the dependent entity, allowing navigation in both directions:

language-csharp
|
public class Order
{
    public int OrderId { get; set; }
    public ICollection<OrderItem> Items { get; set; } = new List<OrderItem>();
}

public class OrderItem
{
    public int OrderItemId { get; set; }
    public string ProductName { get; set; } = null!;
    public Order? Order { get; set; }
}

OrderItem.Order provides access to the principal order; Order.Items provides access to its items. No foreign key is declared in OrderItem, so EF Core still creates a shadow OrderId foreign key. Because OrderItem.Order is nullable, this example is optional with nullable reference types enabled.

Optional One-to-Many Relationship

Instead of a shadow foreign key, declare a nullable foreign key explicitly if an item may temporarily exist without an order—for example, while assembling a draft order:

language-csharp
|
public class Order
{
    public int OrderId { get; set; }
    public ICollection<OrderItem> Items { get; set; } = new List<OrderItem>();
}

public class OrderItem
{
    public int OrderItemId { get; set; }
    public string ProductName { get; set; } = null!;
    public int? OrderId { get; set; }
    public Order? Order { get; set; }
}

EF Core discovers OrderItem.OrderId as the foreign key. Because it is a nullable int?, the relationship is optional. The nullable Order? navigation is consistent with this configuration when nullable reference types are enabled.

Optional relationships use ClientSetNull by convention: EF Core sets the foreign key to null for tracked dependents when the relationship is severed or the principal is deleted. With untracked dependents, the database may reject a principal deletion unless a database-side action has been configured. For more details, see Cascade Delete in EF Core.

One-to-Many with Explicit Foreign Key

In a typical order-processing model, every saved order item belongs to an order. Define a non-nullable OrderId foreign key to make the relationship required:

language-csharp
|
public class Order
{
    public int OrderId { get; set; }
    public ICollection<OrderItem> Items { get; set; } = new List<OrderItem>();
}

public class OrderItem
{
    public int OrderItemId { get; set; }
    public string ProductName { get; set; } = null!;
    public int OrderId { get; set; }
    public Order Order { get; set; } = null!;
}

EF Core discovers the one-to-many relationship by convention. Because OrderItem.OrderId is a non-nullable int, each order item must reference an order, although an order can exist without any items. Required relationships use cascade delete by convention; to change that behavior, see Cascade Delete in EF Core.

Without an Inverse Navigation Property

The dependent entity does not need an Order navigation property when the foreign key is explicit:

language-csharp
|
public class Order
{
    public int OrderId { get; set; }
    public ICollection<OrderItem> Items { get; set; } = new List<OrderItem>();
}

public class OrderItem
{
    public int OrderItemId { get; set; }
    public string ProductName { get; set; } = null!;
    public int OrderId { get; set; }
}

EF Core discovers the required relationship from Order.Items and OrderItem.OrderId; an inverse reference navigation is not required. See the official documentation on one-to-many relationships without navigation to the principal.

⚡ Delete Thousands of Entities Efficiently

Deleting large datasets shouldn't take forever. BulkDelete helps you remove thousands of records without the overhead of regular SaveChanges.

Explore BulkDelete →

Other One-to-Many Configurations

EF Core supports additional one-to-many configurations. The following references cover less common scenarios without expanding the introductory examples.

Configuration When to use it Documentation
Without navigation to dependents The principal does not expose a collection navigation. Microsoft Learn
Without navigations Neither entity exposes navigation properties; explicit configuration is necessary. Microsoft Learn
Alternate key The dependent references an alternate key instead of the principal's primary key. Microsoft Learn
Composite foreign key The dependent uses more than one foreign key property. Microsoft Learn

When conventions do not produce the intended relationship, Fluent API methods such as HasMany(), WithOne(), and HasForeignKey() allow you to configure it explicitly.

External Resources

The following videos demonstrate different ways to define and configure one-to-many relationships in EF Core. Together, they cover explicit foreign keys, shadow foreign keys, required and optional relationships, and navigation properties.

Video 1 - Master One-to-Many EF Core Relationships in 10 Minutes | ASP.NET Core Minimal API Tutorial — Poorna Soysa

Poorna Soysa demonstrates a one-to-many relationship between Customer and Order, using an explicit foreign key, navigation properties, and Fluent API. The example also shows how the relationship is represented in SQL Server after applying a migration.

Key timestamps:

Although EF Core can discover this relationship by convention, Fluent API provides additional control when needed.

Video 2 - Shadow Properties - EF Core - Foreign Keys — Full Stack Amigo

Full Stack Amigo demonstrates how EF Core creates and manages a shadow foreign key in a one-to-many relationship between User and Tweet. The example shows that a foreign key can exist in the EF Core model and database without being declared as a property in the dependent C# class.

Key timestamps:

Shadow foreign keys can be optional or required. Their nullability depends on the relationship conventions and configuration, not simply on whether the foreign key is declared in the C# class.

Video 3 - EF Core 6: How to build and manage relationships (Part 4) — Building SaaS with Gabriel

Building SaaS with Gabriel demonstrates a one-to-many relationship between Grade and Student. The relevant sections show how EF Core initially generates a nullable shadow foreign key and how declaring a non-nullable GradeId property changes the relationship to required. The example also introduces an inverse navigation property.

Key timestamps:

The most useful comparison is between the migrations before and after adding int GradeId. The one-to-many demonstration runs from approximately 4:31 to 11:03; later sections cover other relationship types.

Summary

EF Core supports one-to-many relationships through conventions or explicit configuration. The appropriate configuration depends on whether the relationship is required or optional and which foreign key and navigation properties your entities expose.

  • Shadow foreign keys: EF Core can create a foreign key when no corresponding property is declared in the dependent entity.
  • Optional and required relationships: A nullable foreign key makes the relationship optional, while a non-nullable foreign key makes it required.
  • Navigation properties: Relationships can have navigations in both directions, one direction, or neither.
  • Explicit configuration: Fluent API provides control when conventions do not match the intended relationship.
🔄 Insert or Update in One Operation

Need an upsert in EF Core? BulkMerge automatically inserts new entities and updates existing ones, even with custom keys.

Explore BulkMerge →

FAQ

Can a shadow foreign key be required?

Yes. A shadow foreign key can be required even when no foreign key property is declared in the dependent entity. Use IsRequired() in Fluent API to configure a required relationship when EF Core would otherwise treat it as optional.

Does a required one-to-many relationship mean the principal must have dependents?

No. A required relationship means each dependent must be associated with a principal. It does not require the principal to have any dependents. For example, an order can exist without items, even when every order item must belong to an order.

Are navigation properties required for a one-to-many relationship?

No. A one-to-many relationship can have navigations on both sides, on only one side, or on neither side. EF Core can discover many relationships by convention when suitable navigations are present. Without navigation properties, the relationship must be configured explicitly.

No. A non-nullable foreign key makes the relationship required, but it does not automatically load the related entity into its navigation property. You can use Include() to load related data as part of a query. See Eager Loading in EF Core for more details.


Date Modified: 2026-11-23
Author:

Edit this page in GitHub

Got any EF Core Question?