Updated February 14, 2023
Introduction to Entity Framework Navigation Properties
Entity Framework Navigation Properties is an optional property in entity type which enables the navigations from one end to another end of associations. The Navigation Property is used to map the relations; it does not carry the data. Instead, the navigation Property defines the name, the association it navigates, and the end of the association it navigates. Navigation Properties are optional at both entity types at the lots of an association.
Overview of Entity Framework Navigation Properties
The Entity Framework Navigation Properties are the optional case at the two entity types at both ends of an association. Suppose we define the navigation property on one entity type at the end of the association. In that case, we do not need to determine the navigation property on the entity type at the other end of the association. The Navigation Property is used to map the relation. Consider one example Order in which the OrderDetails fetches the entire OrderDetails of specific orders, like which customer bought, what the products are at, what the cost rate is, and so on.
Loading the navigation properties in Entity Framework enables the use of the navigation properties in the model to the related entities. In this, three general ORM prototypes load the associated data. The example below explains the conceptual model with three entity types: Book, Author, and Publisher. The Navigation Properties Publisher and Authors are defined on the entity type Book. The Navigation Property Books are defined on two entity types, Publisher and Author.
Let’s see the following diagram below as shown:
The Entity Framework ADO.NET uses the DSL (Domain Specific Language), the (CSDL) Conceptual Schema Definition Language; it describes the conceptual models.
How does Entity Framework Navigation Properties works?
The Navigation Property is the property defined on the standard or the dependent entity which encloses the reference to the related entity. It represents the relationship between both entity types. They enable us to navigate from one relationship’s end to the other. For example, the Entity Framework relationship must have two endpoints; both endpoints take part in the relationship and should return the navigation property, which depicts the relationship.
Let’s see one example; consider the entity models Employee and Department.
Code:
public class Employee
{
public int Em_ID { get; set; }
public string Em._Name { get; set; }
public Department Department{ get; set; }
}
public class Department
{
public int Dp_ID { get; set; }
public string Dp_Name { get; set; }
public ICollection < Employee > Employees { get; set; }
}
In the above code, the property of the Department present in the Employee Class (which is the dependent entity) holds the reference to the Department Class (which is the Standard entity) as the relationship at one end. However, in the Department Class, there is an Employees Collection which is part of another end of the relationship.
Let’s see the below representation:
In navigation property, entity types can return two types depending on the relationship to which they contribute.
- One is Reference Object (zero-or-one relationship); consider the above example, the Department property in the Employee Class.
- Another one is a collection (relationship is many); consider the above example of Employee Collection in the Department Class.
The Navigation Property is used to map the relations. The association between the entities describes three things: referential integrity, type of association, and the type of entity when we use the navigation property in the application code, which defines that it asks the Entity Framework to perform the joins automatically between two tables.
Types of Entity Framework Navigation Properties
Navigation Property includes the name, the association it navigates, and the end of the association it navigates. Navigation Properties are optional at both entity types at the ends of an association. The initial part defines the navigation property on a current entity, and the second part describes the reverse navigation property.
Let’s see the types of the navigation property:
- Optional Relationship: It is a nullable foreign key, and the diversity like 0. . 1 may be (Zero to One) (One to One relationship).
- Required Relationship: It is the One to Many Relationship.
- Many Relationship: It is the Many to Many Relationship.
1. Optional Relationship
This is the Optional Relationship, which is a nullable foreign key, and the diversity like 0. 1 may be (Zero to One) (One to One relationship). The Primary Key value contains the record, and its related table contains the records related to the table. So this relationship is called the One to One relationship. Let’s see one example Client and their address which has a one-to-one relationship. It means that the customer can have either one or zero addresses. So create the class of customer having the attributes of ID and name and make one more class, CustomerAddress, which contains the ID, Address, and City.
Code:
class Customer
{
public int CustomerID{get;set;};
public String Name{get;set;};
}
class CustomerAddress
{
public int CustomerAddressId {get;set;}
public String Address {get;set;}
public string City {get;set;}
[ForeignKey ("Customer")]
public int FkCustomer {get; set;}
public Customer Customer{get;set;} // it is the Navigation Property.
}
2. Required Relationships
The primary key value table holds one record and their table, which contains many, zero, and one. This relationship is called the One-to-Many Relationship. Let’s see one example: Customer and Project entity, a single customer has several projects, but the particular project can have only one relationship with one project. Just note that the reverse navigation property expression is defined as the collection; if there are one to many relationships, follow the code below.
Code:
class Customer
{
public int CustomerID{get;set;}
public String CustomerName{get;set;}
// it is the reverse navigation property which is Second Part of expression
public virtual ICollection<Project> Projects { get; set; }
}
class Project
{
public int ProjectID{get;set;}
public String ProjName {get;set;}
// First Part of expression define navigation property
public Customer Customers {get;set}
}
3. Many Relationship
The Many-to-Many relationship is related to any number of records of another table, and the second table is associated with any number of records of the first table. This relationship requires the third table, called mapping or linking the table. For example, let’s see the relationship between the entity Student and the course. Here one student is registered for several courses, and one course can be explained to many students.
Code:
class StudentMaster
{
public int St_Id {get;set;}
public String St_Name { get; set; }
public String St_Address { get; set; }
public virtual ICollection<CourseMaster> CourseMasters { get; set; }
}
class CourseMaster
{
public int Cr_Id {get; set;}
public String Cr_Name { get; set; }
public virtual ICollection<StudentMaster> StudentMasters { get; set; }
}
Conclusion
In this article, we have seen the Navigational Properties in Entity Framework, which describes that the properties are the method to represent the foreign key relationship in the database or it describes the relationship between two entities.
Recommended Articles
This is a guide to Entity Framework Navigation Properties. Here we discuss the introduction and how to work entity framework navigation properties & types. You may also have a look at the following articles to learn more –