Entity Framework Core Interview Question #27

What is a navigation property in EF Core?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

A navigation property represents a relationship between entities in the object model. A reference navigation points to one related entity, while a collection navigation points to multiple related entities.

Detailed Explanation

A navigation property represents a relationship between entities in the object model. A reference navigation points to one related entity, while a collection navigation points to multiple related entities.

Navigation properties allow LINQ queries and tracked entity graphs to work with related objects rather than manually handling foreign-key values everywhere.

Navigation properties do not automatically mean related data is loaded. Whether it is available depends on the query and the loading strategy used.

Code Example

public class Order
{
    public int Id { get; set; }

    public int CustomerId { get; set; }
    public Customer Customer { get; set; } = null!; // Reference navigation

    public ICollection<OrderItem> Items { get; set; }
        = new List<OrderItem>(); // Collection navigation
}