Entity Framework Core Interview Question #29
What is a one-to-many relationship in EF Core?
Relationships, Navigation Properties & Loading Data Junior Beginner
Quick Interview Answer
A one-to-many relationship means one principal entity can be associated with many dependent entities, while each dependent is associated with one principal.
Detailed Explanation
A one-to-many relationship means one principal entity can be associated with many dependent entities, while each dependent is associated with one principal.
For example, one Category can contain many Products. The Product normally stores the CategoryId foreign key, while Category exposes a collection navigation.
This is one of the most common relational patterns in business applications.
Code Example
modelBuilder.Entity<Product>()
.HasOne(p => p.Category)
.WithMany(c => c.Products)
.HasForeignKey(p => p.CategoryId);