Data Annotations InverseProperty Attribute EF 6 & EF Core

The InverserProperty informs the EF, which navigational property it relates to on the other end of the relationship. we use it when there are multiple relationships between two entities. The default convention fails to detect multiple relationships.

InverseProperty

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

Consider the following model

Data Annotations ForeignKey Attribute in EF and 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 default Convention fails to detect multiple 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.

The above works only in EF 6. The EF Core generates an error

InverseProperty Attribute

This problem can be solved by using the Data annotations InverserProperty attribute on any one side of the relationships. Apply InverseProperty attribute on a property and specify the corresponding navigational property of the other end of the relationship.

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

Now the EF matches the relationship correctly and creates only two fields as shown in the image below. Note that EF 6 & EF Core a slightly different way of naming the Foreign Key.

Data Annotations InverseProperty in EF And EF Core

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

References

  1. Relationships in Entity Framework
  2. One to one relationship in Entity Framework
  3. One to Many relationships in Entity Framework
  4. Many to Many relationships in Entity Framework
  5. Relationships in Entity Framework Core
  6. One to One Relationships in Entity Framework Core
  7. One to Many Relationships in Entity Framework Core
  8. Many To Many Relationships in Entity Framework 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