InversePropertyAttribute in System.ComponentModel.DataAnnotations.Schema can be quite handy when using Entity Framework code first. Let’s take the following scenario.
Consider that you have the following two entities and the data context.
When a database is created from the above model, you will be getting a database like below.
There is nothing wrong with this model. Since we have exposed “People” property of type List<Person> in the “Hobby” entity, Entity Framework is creating a foreign key inside “Person” entity named “Hobby_Id” along with the foreign keys “PrimaryHobby_Id” and “SecondaryHobby_Id”.
But what if this is not what you want. Instead, Let’s say when you want to get all the people having a specific hobby, you only want to get the people in who’s primary hobby is that particular hobby. For that there should be a relationship between “People” property in “Hobby” entity and “PrimaryHobby” property in “Person” entity. And that’s where the InversePropertyAttribute comes in.
Now let’s modify the “Person” entity decorating the “PrimaryHobby” property with the InversePropertyAttribute.With that change, the underlined database get changed as follows.
There is no “Hobby_Id” property anymore, the navigation from “Hobby” to “People” will be done through “PrimaryHobby_Id”.
The same thing can be done through Entity Framework Fluent API.So that’s it. Please find the code sample in my OneDrive.