Entity Framework Core Interview Question #28
What is a foreign key property in EF Core?
Relationships, Navigation Properties & Loading Data Junior Beginner
Quick Interview Answer
A foreign key property stores the key value that links a dependent entity to its principal entity. EF Core can often discover foreign keys by conventions such as CustomerId for a Customer navigation.
Detailed Explanation
A foreign key property stores the key value that links a dependent entity to its principal entity. EF Core can often discover foreign keys by conventions such as CustomerId for a Customer navigation.
Keeping the foreign-key property in the entity can make disconnected web scenarios easier because the relationship can be changed using the key without loading the related entity.
Foreign keys can also be shadow properties when the key is not represented explicitly in the CLR entity class.
Code Example
public class Order
{
public int Id { get; set; }
public int CustomerId { get; set; } // Foreign key
public Customer Customer { get; set; } = null!;
}