ForeignKey Attribute in EF & EF Core

In this tutorial learn to use the ForeignKey Attribute Entity Framework & 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 automatically creates the Foreign Key field in the database for us.

In EF 6, if we did not include the Foreign key field, the Entity Framework conventions will use the format NavigationpropertyName_PrimaryKeyOftheNavigationPropertyType. While in the case of EF Core Conventions, it will use the Primary key field of the Principal class.  

Best Entity Framework Core Books

Best Entity Framework Books

Foreign Key Default Convention in EF 6 and EF Core
Foreign Key Default Convention in EF 6 and EF Core

But, if you add the DepartmentID property in the Employee entity, Entity Framework conventions will use that field.

Foreign Key Default Convention in EF 6 and EF Core

ForeignKey Attribute

What if we wish to change the DepartmentID Property to DeptID in the employee table. The Default Convention of both EF & EF Core will not recognize the DeptID Property and will create Department_DepartmentID (or DepartmentID in EF Core) 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.

Where
Name:  The name of the associated navigation property or of the associated foreign keys

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
Foreign Key Attribute on Foreign Key property property of the dependent entity

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

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 dependent and principal entity
Foreign Key Attribute on navigational property of the dependent and principal entity

Reference

  1. ForeignKeyAttribute Class
  2. Data Annotations in Entity Framework & Entity Framework Core
  3. Entity Framework Code First Conventions
  4. Entity Framework Core Conventions
  5. One to One Relationship in Entity Framework Core
  6. One to Many Relationship in Entity Framework Core
  7. Many to Many Relationship in Entity Framework Core
  8. One to One Relationship in Entity Framework
  9. One to Many Relationship in Entity Framework
  10. Many to Many Relationship in Entity Framework

1 thought on “ForeignKey Attribute in EF & EF Core”

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