Entity Framework Core Interview Question #26

What is a relationship in EF Core?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

A relationship describes how two entity types are associated. EF Core represents relationships using primary keys, foreign keys, and navigation properties.

Detailed Explanation

A relationship describes how two entity types are associated. EF Core represents relationships using primary keys, foreign keys, and navigation properties.

Common relationship types are one-to-one, one-to-many, and many-to-many. EF Core can discover many relationships by convention, while the Fluent API provides explicit control when conventions are insufficient.

Correct relationship configuration is important because it affects foreign-key constraints, generated SQL, loading behavior, cascade deletes, and how entity graphs are tracked.

Code Example

public class Category
{
    public int Id { get; set; }
    public ICollection<Product> Products { get; set; } = new List<Product>();
}

public class Product
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; } = null!;
}