ForeignKey Attribute in Entity Framework

In this tutorial learn to use the ForeignKey attribute in Entity Framework to configure the Foreign Key Property. We use the Foreign Key to define the relationship between two 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 conventions will create the Foreign Key field in the database for us. It will use the name NavigationpropertyName_PrimaryKeyOftheNavigationPropertyType.

Foreign Key Default Convention in Entity Framework
Foreign Key Default Convention in EF 6

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

Foreign Key Default Convention in EF

ForeignKey Attribute

What if we wish to change the DepartmentID Property to DeptID in the Employee table. The Default Convention in EF will not recognize the DeptID Property and will create Department_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 Entity Framework
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

Foreign Key Attribute on Navigational Property of the dependent class in Entity Framework

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

Reference

  1. ForeignKeyAttribute Class

Read More

  1. Entity Framework Tutorial
  2. Configure the Entity Data Model
  3. Code First Conventions in Entity Framework
  4. Data Annotations in Entity Framework
  5. Key Attribute
  6. Fluent API in Entity Framework
  7. Relationships in Entity Framework
  8. One to one relationship in Entity Framework
  9. One to Many relationships in Entity Framework
  10. Many to Many relationships in Entity Framework

2 thoughts on “ForeignKey Attribute in Entity Framework”

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