ForeignKey Attribute in EF Core

In this tutorial learn to use the ForeignKey Attribute in Entity Framework Core to configure the Foreign Key Property. We use the Foreign Key to define the relationship between tables in the database. For Example, the Employee working in a Department is a relationship. We express this relationship by creating a DepartmentID field in the Employee table and marking it as Foreign Key. In this example, the Department is the Principal entity & Employee is a Dependent entity.

Foreign Key Default Convention

In the following model, the Entity Employee has a Department navigational property that links it to the Department entity. We do not have any Property representing the Foreign Key field in Employee entity. The Entity framework Core automatically creates the Foreign Key field in the database for us.

the Entity Framework Core conventions will use the name of the Primary key field of the Principal class for the Foreign Key field   

Foreign Key Default Convention in EF Core
Foreign Key Default Convention in EF Core

But, if you add the DepartmentID property in the Employee entity, Entity Framework Core conventions will make use of that field and will not create another field.

ForeignKey Attribute

What if we wish to change the DepartmentID Property to DeptID in the employee table. The Default Convention in EF Core will not recognize the DeptID Property and will create DepartmentID column in the database.

We can override this behavior using the Foreign key attribute on the navigational property. The following is the syntax of the ForeignKey Attribute.

There are three ways you can apply this attribute

  • ForeignKey property of the dependent class
  • Navigational Property of  the dependent class
  • Navigational Property of the Principal class

In the example above example, Employee is the dependent class as it depends on the Department. The Department is the Principal class.

Foreign Key property of the dependent class

The following example, we apply the ForeignKey attribute on the DeptID Property of the Employee class. In that case, the name must point to the navigational property

Foreign Key Attribute on Foreign Key property property of the dependent entity in EF Core

Navigational Property of  the dependent class

We can also place the ForeignKey Attribute Navigation property. When placed on navigation property, it should specify the associated foreign key

Foreign Key Attribute on Navigational Property of the dependent class in EF Core

Navigational Property of the Principal class

We can also place the Foreign key attribute on the Navigational property of the Principal class. The name argument must point to the Foreign Key of the dependent class.

Foreign Key Attribute on Navigational Property of the Principal class in EF Core
Foreign Key Attribute on navigational property of the dependent and principal entity

Reference

  1. ForeignKeyAttribute Class

Read More

  1. Entity Framework Core Tutorial
  2. Conventions in Entity Framework Core 
  3. Data Annotations in entity framework Core 
  4. Key Attribute
  5. Fluent API Entity Framework Core
  6. Relationships & Navigational Properties
  7. One to One Relationships
  8. One to Many Relationships
  9. Many To Many Relationships

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