Entity Framework Core Interview Question #30

What is a one-to-one relationship in EF Core?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

A one-to-one relationship means one entity is associated with at most one related entity on the other side.

Detailed Explanation

A one-to-one relationship means one entity is associated with at most one related entity on the other side.

EF Core must know which side is the dependent because the dependent normally contains the foreign key. The Fluent API is often used to make this explicit.

A common example is a User and UserProfile relationship where one profile belongs to one user.

Code Example

modelBuilder.Entity<User>()
    .HasOne(u => u.Profile)
    .WithOne(p => p.User)
    .HasForeignKey<UserProfile>(p => p.UserId);