InverseProperty Attribute in EF Core

The InverseProperty informs the EF Core, which navigational property it relates to on the other end of the relationship. The default convention in EF Core correctly identifies only if there is a single relation between two entities. But in the case of multiple relationships, it fails to correctly identify them. In such cases, we use the InverseProperty to help the EF core correctly identify the relationship.

InverseProperty

A relationship in the Entity Framework Core 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 Core. The Entity Framework by convention detects this relationship and creates the appropriate Foreign Key column.

Consider the following model

ForeignKey Default Convention in EF Core

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

Best Entity Framework Core Books

Best Entity Framework Books

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

The EF Core, throws the following error, when we run the migrations

Unable to determine the relationship represented by navigation `Airport.DepartingFlights’ of type ‘ICollection’. Either manually configure the relationship, or ignore this property using the ‘[NotMapped]’ attribute or by using ‘EntityTypeBuilder.Ignore’ in ‘OnModelCreating’.

Using InverseProperty

We solve this problem by using the InverseProperty attribute on any one side of the relationships. Apply InverseProperty attribute on that property. Specify the corresponding navigational property of the other end of the relationship as its argument.

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

Using Inverse Property in EF Core

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

Handling Multiple Relationships using InverseProperty in Entity Framework Core

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

References

  1. InversePropertyAttribute Class

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