InverseProperty Attribute Entity Framework

The InverseProperty informs the EF, which navigational property it relates to on the other side of the relationship. If two entities have a single relationship between them, the EF convention correctly identities them. But it fails when there are multiple relationships between entities. In such cases, we use the InverseProperty to help the EF correctly identify the relationship.

Default Convention

A relationship in the Entity Framework always has two endpoints. Each end must return a navigational property that maps to the other end of the relationship. you can read about it from the tutorial relationships in Entity Framework. The Entity Framework by convention detects this relationship and creates the appropriate Foreign Key column.

Consider the following model

Entity Framework Creates Foreign Key Using Convention

In the above example, we have employee belonging to a particular department. The default convention automatically detects the relationship and creates the DepartmentID foreign key column in the Employee table.

Multiple Relations

The employee and department is a single relationship. What if the employee belongs to multiple departments ?. Let us take the example of flight & airports. Flight departing from one airport and arrives at another. So the flight has multiple relationships with the Airport

The DepartngFlights property must map to DepartureAirport property in the airport model and ArrivingFlights property must map to ArrivalAirport property.

The default Convention in EF fails to detect such relationships. There is no way EF can deduce which navigational property maps to which property on the other end. It will assume that these are four different relationships and creates the four columns in the Flight table as shown in the image below.

Entity Framework Fails to detect multiple relationships

Using InverseProperty

The InverseProperty data annotation attribute can solve the above problem.

We apply the InverseProperty to a property on any one side of the relationship. We specify the related navigational property from the other end of the relationship as its argument.

The Airport class after applying the InverseProperty is as shown below.

Using Inverse Property in Entity Framework

Now the EF matches the relationship correctly and creates only two fields as shown in the image below.

Creating Foreign Key using Inverse Property in Entity Framework

You can apply InverseProperty on the Flight instead of the airport as shown below. Both will result in the same output

References

  1. InverseProperty API

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