Navigation Property in EF Core
A navigation property in EF Core is a property in an entity that represents a relationship with another entity. These properties allow EF Core to navigate relationships and automatically load related data.
Navigation properties are essential for managing entity relationships and
enable efficient data loading. Relationships can be configured using Fluent API
or data annotations.
Navigation Properties Types:
In EF Core, navigation properties are used to define relationships between
entities. These properties allow EF Core to traverse those relationships and
load related data. There are three primary types of navigation properties:
1. Reference Navigation Property
2. Collection Navigation Property
3. Many-to-Many Navigation Property
1.
Reference Navigation Property
A reference navigation property is used for one-to-one or many-to-one
relationships, where an entity has a reference to a single related entity.
This property allows you to navigate to a single related entity from the
current entity.
For example, A Customer has one Order (one-to-one), or an Order belongs to a
single Customer (many-to-one).
public class Order
{
public int OrderId { get; set; }
public string Product { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; } // Reference
navigation property
}
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public ICollection<Order> Orders { get; set; } //
Collection navigation property (for many-to-one)
}
Explanation: In this case, the Order entity has a reference
navigation property to the Customer, indicating that each order is associated
with a single customer.
Collection Navigation Property
A collection navigation property is used for one-to-many or many-to-many
relationships, where one entity can be related to multiple instances of another
entity.
This property is typically used to represent a collection (e.g., a list) of
related entities.
For example, A Customer can have many Orders (one-to-many), or a Course can
have many Students (many-to-many).
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public ICollection<Order> Orders { get; set; } //
Collection navigation property
}
public class Order
{
public int OrderId { get; set; }
public string Product { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
Explanation: The Customer class has a collection navigation
property, Orders, which represents the one-to-many relationship. This allows
accessing all orders associated with a customer.
Many-to-Many Navigation Property
A many-to-many navigation property is used for many-to-many relationships,
where many instances of one entity can be related to many instances of another
entity.
In EF Core, this relationship is often represented through a join entity
(many-to-many table), which links the two entities together.
For example, A Student can enroll in many Courses, and a Course can have many
Students.
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public ICollection<StudentCourse> StudentCourses { get;
set; } // Many-to-many navigation property
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public ICollection<StudentCourse> StudentCourses { get;
set; } // Many-to-many navigation property
}
public class StudentCourse
{
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}
Explanation: In this many-to-many relationship, the StudentCourse
class acts as a join entity, with each student being able to enroll in many
courses, and each course having many students. Both Student and Course have a
collection navigation property to StudentCourse.
Comments
Post a Comment