Entity Framework Core Interview Question #33

What are principal and dependent entities in EF Core relationships?

Relationships, Navigation Properties & Loading Data Junior Beginner

Quick Interview Answer

The principal entity is the entity whose key is referenced by the relationship. The dependent entity contains the foreign key that points to the principal.

Detailed Explanation

The principal entity is the entity whose key is referenced by the relationship. The dependent entity contains the foreign key that points to the principal.

For example, if Product.CategoryId references Category.Id, Category is the principal and Product is the dependent.

Understanding these roles is especially important for one-to-one relationships, required versus optional relationships, and cascade delete behavior.

Code Example

// Category = principal
// Product = dependent

modelBuilder.Entity<Product>()
    .HasOne(p => p.Category)
    .WithMany(c => c.Products)
    .HasForeignKey(p => p.CategoryId);