In this tutorial, we will learn how to Configure One to Many relationships in Entity Framework Core. You can use EF Core Conventions, Data Annotations or Fluent API to configure the One to Many relationships.
Table of Contents
One to Many Relationships in EF Core
Let us take an example of the relationship between the employee and the department to which employee belongs to. The relationship between the employee and the department is One to Many. The employee belongs to only one department. The department can have many employees. In a One to Many relationship Primary key of the department table (DepartmentID) is defined as the Foreign key in the Employees table
Our Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Employee { public int EmployeeID { get; set; } public string Name { get; set; } //Navigational Property public Department Department { get; set; } } public class Department { public int DepartmentID { get; set; } public string Name { get; set; } //Navigational Property public ICollection<Employee> Employees { get; set; } } |
Note that navigational property in the employee class returns the reference to the department object. The navigational property in Department class returns the employees collection.
One to many relationship Using EF Core Convention
The EF Core conventions use the Navigational property to determine which class is dependent on which. The above model mapped to the database as shown in the image below
Note that
- The DepartmentID Property is added to the employee table by the EF Core.
- This Field is marked as Foreign Key.
- DepartmentID is created as Nullable Column
Foreign key Property
The above model created the Foreign Key DepartmentID in the Employee Table. You can include the DepartmentID Property in the dependent class (Employee) as shown below
1 2 3 4 5 6 7 8 9 10 | public class Employee { public int EmployeeID { get; set; } public string Name { get; set; } public int DepartmentID { get; set; } public Department Department { get; set; } } |
The EF Core will generate the DepartmentID as Not Nullable column
If you want Nullable DepartmentID column, then define the property as Nullable as shown in the code below
1 2 3 | public int? DepartmentID { get;set;} |
One to many Relationship Using Data Annotations
The default convention makes an excellent work inferring the model and creates the necessary relationships. But the default conventions work only if you follow the conventions correctly. For Example in the previous example, if you add DeptID instead of DepartmentID then the EF Core fails to detect the relationship.
This can be overridden by using the ForeignKey attribute on the navigational property as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Employee { public int EmployeeID { get; set; } public string Name { get; set; } [ForeignKey("Dept")] public int DeptID { get;set;} public virtual Department Dept { get; set; } } public class Department { public int DepartmentID { get; set; } public string Name { get; set; } public virtual ICollection<Employee> Employees { get; set; } } |
Here DeptID is created as Not Nullable Column.
You can refer to the ForeignKey
One to many Rrelationship Using Fluent API
One to many relationships using Fluent API is done using the HasOne and WithMany methods of the EntityTypeBuilder object as shown below
1 2 3 4 5 6 | modelBuilder.Entity<Employee>() .HasOne<Department>(e => e.Department) .WithMany(d => d.Employees) .HasForeignKey(e => e.DeptID); |
First, we start by configuring the Employee Entity
The Employee has Department Navigational property, which stands for “One” relationship, Hence we will use HasOne method. We pass the navigation property of the Employee class to it
Next, we refer to the other end of the relationship i.e. Department, which is “Many” Side of the relation. Hence we use WithMany method. We pass the Navigation property o of the Department class (i.e Employees collection)
Finally, we let the EF know that DeptID is to be used as the ForeginKey using the HasForeignKey method
The above code creates a NOT Null column
To Create a Null able Column you can either use
1 2 3 | public int? DeptID { get; set; } |
or use the isRequired method as shown below
1 2 3 4 5 6 7 | modelBuilder.Entity<Employee>() .HasOne<Department>(e => e.Department) .WithMany(d => d.Employees) .HasForeignKey(e => e.DeptID) .IsRequired(false); |
Conclusion
The above sets up the One to Many Relationship using Data annotations & Fluent API. In the next tutorial let us look at how to Configure the Many to Many Relationship
What is the version of EF Core used in these examples?