EF Core WithOne

The Entity Framework Core Fluent API WithOne method is used to configure the one side of a one-to-many or one-to-one relationship.

The WithOne method must be used in conjunction with the HasOne (one-to-one) or HasMany (one-to-many) method to fully configure a valid relationship, adhering to the Has/With pattern for relationship configuration.

The following model represents companies and employees with an inverse navigation property defined in the dependent entity (Employee):

🛠️ Take Full Control of EF Core Bulk Operations

Custom keys, conditional updates, selected columns, and more. EF Extensions offers 100+ options to customize how your entities are saved.

Explore Bulk Operations →

One-To-Many Example

language-csharp
|
public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Employee> Employees { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Company Company { get; set; }
}

A company has many employees, each with one company. That relationship is represented as follows:

language-csharp
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Employee>()
        .HasOne(e => e.Company)
        .WithMany(c => c.Employees);
        
}

Data Annotations

Relationships can be defined with the ForeignKey and InverseProperty attributes.

Further Reading


Date Modified: 2026-04-08
Author:

Edit this page in GitHub

Got any EF Core Question?