In this lesson, you will learn how to map relationships between database tables by defining navigation properties directly within your C# entity classes.
The Power of Navigation Properties
In Entity Framework Core, navigation properties are class members that allow you to navigate from one entity to another. Instead of manually writing foreign key queries, EF Core handles the relationship for you through these strongly-typed links.
One-to-Many Relationships
A common scenario is a Blog that contains many Post objects. To establish this, you place a collection on the parent entity and a reference back on the child:
public class Blog
{
public int BlogId { get; set; }
// Navigation property: One Blog has many Posts
public ICollection<Post> Posts { get; set; } = new List<Post>();
}
public class Post
{
public int PostId { get; set; }
public int BlogId { get; set; }
// Navigation property: Many Posts belong to one Blog
public Blog Blog { get; set; }
}
By including the Blog property inside Post, EF Core recognizes the relationship. It uses the BlogId as a foreign key and automatically links the objects in memory when you query them.
One-to-One Relationships
If you need a one-to-one relationship—for example, a User having exactly one Profile—you simply define a single reference on both sides:
public class User
{
public int UserId { get; set; }
public UserProfile Profile { get; set; }
}
public class UserProfile
{
public int UserProfileId { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
Why Use Navigation Properties?
Navigation properties turn database "joins" into C# object traversal. When you load a Blog from the database, EF Core can "include" the related Posts collection in a single operation. This keeps your domain code clean, readable, and focused on object relationships rather than relational database IDs.
Key Takeaway: Navigation properties allow you to model database relationships as C# object references, enabling intuitive traversal of your data graph while letting EF Core manage the underlying foreign key synchronization.