One to Many Relationship in Entity Framework Core

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. 

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

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

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

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.

Here DeptID is created as Not Nullable Column.

You can refer to the ForeignKey Data annotation Attribute for more information on ForeignKey Attribute.

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

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

or use the isRequired method as shown below

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

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top