Entity Framework Core Interview Question #34

What is a required relationship in EF Core?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

A required relationship means the dependent must be associated with a principal. For a relational database, the foreign-key column is normally non-nullable.

Detailed Explanation

A required relationship means the dependent must be associated with a principal. For a relational database, the foreign-key column is normally non-nullable.

A non-nullable foreign-key property such as int CategoryId commonly indicates a required relationship.

Requiredness should reflect the actual business rule. Making a relationship required only to simplify code can lead to invalid domain assumptions.

Code Example

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

    public int CategoryId { get; set; } // Required FK
    public Category Category { get; set; } = null!;
}