One to One Relationship in Entity Framework Core

We are going to look at how to Configure one to one relationship in entity framework core using Convention, Data Annotations & Fluent API.

One to One relationship in Entity Framework Core

Let us take the example of an Employee and EmployeeAddress domain models and create a One to One relationship between them. In a One to one relationship PrimaryKey of the Primary table (employeeID of employee table) is both Primary key and Foreign key in the dependent table (EmployeeAddress).

Basic Model

Note that the we have created the navigational property which returns the reference to the related entity. In Employee class navigational property returns the reference to the EmployeeAddress object. In EmployeeAddress class navigational property returns the reference to the Employee object.

One to One Relationship using Default Convention

The Navigational property in both the classes returns the object. This makes it difficult for the Entity framework, to determine which class is dependent on which. Hence Setting up the One to One Relationship using default convention is not straightforward.

The EmployeeAddress class uses the EmployeeID as both Primary key & Foreign Key. The Convention requires us to have either Id property or EmployeeAddressID Property in the EmployeeAddress class

So by Adding the public int id { get; set; } we are able to trick EF Core to create the one to one relationship for us

One to One Relationship Convention in EF Core

One to One Relationship using Data Annotations

In Convention, we added id property into the EmployeeAddress table to trick EF Core to set up the relationship for us. The Data Annotations has ForeignKey and Key Attributes which you can use to create the relationships.

Hence we use Primary Key Attribute on EmployeeID property in EmployeeAddress class (Dependent). Our EmployeeAddress class will look like this

One to One Relationship using key Attribute

Now, what if you want to change the name of the EmployeeID to EmpID. In that case, we need to use the ForeignKey Attribute as shown below

One to One Relationship using key and ForeignKey Attribute

One to One Relationship using Fluent API

The Fluent API Provides four methods to define the navigational properties

  • HasOne
  • HasMany
  • WithOne
  • WithMany

The One/Many signifies one or many relationships.

We always begin with HasOne/HasMany on the entity on which you are configuring. We then chain it with WithOne/WithMany to configure the other side of the relationship

Now, let us use the Fluent API to configure the one to one relationship. Remove all the conventions & Data Annotations attributes applied.

Since, we do not have Primary Key defined on EmployeeAddress class, let us define it first using HasKey method

Next, we start by configuring the Employee entity.

Since the Employee class is belongs to “One” side of the relation, we will need HasOne method. The HasOne requires us to specify the Navigation Property (i.e. EmployeeAddress) of the Employee Class (i.e. entity being configured)

Finally, we move one to the other end of the relationship (i.e EmployeeAddress), which is also “one” side of the relation. Hence we will use WithOne. WithOne requires us to specify the Navigation property of the EmployeeAddress class

The Final Code 

One to One Relationship using HasOne/WithOne Using Fluent API

In the example above, we started with the Employee class. We can also start with the EmployeeAddress class to achieve the same result as shown below

Using Foreign Key Field Name

We can use a another field for Foreign Key. For Example consider our EmployeeAddress class as 

We invoke the HasForeignKey method to sepecify the EmpID As the ForeignKey

Conclusion

The above sets up the One to One Relationship using Data annotations & Fluent API. In the next tutorial let us look at how to Configure the One to Many Relationship

1 thought on “One to One Relationship in Entity Framework Core”

  1. how to do as command below in ef core

    UPDATE table1 SET filed1=’value1′,field1=’value2′ WHERE id=1 AND id2=4

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top