How To Use Foreign Key & Inverse Property In Entity Framework?

How To Use Foreign Key & Inverse Property In Entity Framework?2017-11-242017-12-05https://www.loginworks.com/wp-content/uploads/2017/07/mainlogo.pngLoginworks Softwareshttps://www.loginworks.com/wp-content/uploads/2017/11/how-to-use-foreign-key-inverse-property-in-entity-framework.jpg200px200px

Do you know that when you have more than one foreign key relation with the same table then you write Inverse Property just above the Navigation Property? Ok, now this might be quite a direct approach to my blog post. Hence, in order to make it sound somewhat light, let’s first take a revision of the basics…

What Is Navigation Property?

This is the first question that arises after going through the very line of the article. So, let’s dig more into its details!

Navigation properties offer a smooth way to navigate an association between the two entity types. It further allows one to navigate and manage relationships in both of the directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many).

Entity Framework has an option to make only one navigation property for that you will need to define navigation relation in one class only not in both.

Navigation properties are Entity Frameworks way of representing Foreign Key relationships inside the database. Navigation properties allow you to define relationships between entities (rows in your database) in a way that makes sense in an object-oriented language. Consider the following database:

How Does Entity Framework Detect Navigation Properties?

When entity framework examines a class which is attached to the context it finds other complex type properties on the class and assumes that they are a foreign key to that table. Entity framework then creates a foreign key with the name [PropertyName]_[PropertyIdName] for example in the case of the post class the Author is [PropertyName] and in the user table Id is the Id. This means Author_Id is generated as the foreign key name.The rules around navigation properties what’s acceptable and how they are generated are defined by a set of conventions.

I’m not going to go into any more detail on how these work but for more information, you can take a look at the following pages:

EF Feature CTP5: Pluggable Conventions

MSDN – System.Data.Entity.ModelConfiguration.Conventions Namespace

Entity Framework Navigation Property generation rules

Foreign Key Attribute

publicclassStudent

{

publicint StudentID { get; set; }

publicstring StudentName { get; set; } //Foreign key for Standard

publicint StandardId { get; set; }

publicStandard Standard { get; set; }

}

publicclassStandard {

publicint StandardId { get; set; }

publicstring StandardName { get; set; }

publicICollection<Student> Students { get; set; }

}

As you can see in the above code, Student class includes foreign key StandardId, which is key property in Standard class. So now, Code First will create StandardId column in Students class as shown below.

ForeignKey is overriding this. You can have a different name of a foreign key property name than the primary key of the second class.

publicclassStudent

{

publicint StudentID { get; set; }

publicstring StudentName { get; set; } //Foreign key for Standard

publicint StandardRefId { get; set; }

[ForeignKey(“StandardRefId”)]

publicStandard Standard { get; set; }

}

publicclassStandard

{

publicint StandardId { get; set; }

publicstring StandardName { get; set; }

publicICollection<Student> Students { get; set; }

}

As seen in the above example, Student class has StandardRefId foreign key property name instead of StandardId. We defined it using the Foreign Key attribute on Standard navigation property so that Entity Framework’s Code-First can consider StandardRefId as it is foreign key, see below:

It’s not necessary to define the foreign key attribute to navigation property only but we can also put it in the normal properties of class as well. See below:

As you can see in the above example, Student class includes two navigation properties to the Standard class. The same way Standard class includes two collection navigation properties for Student. Code-First creates four columns for this relationship, as shown below.

Inverse Property overrides this convention and specifies the alignment of the properties. The following example uses Inverse Property in Standard class to fix this problem.

As you can see in the above example, we have applied the Inverse Property attribute to CurrentStudents & PreviousStudents property and specify which reference property of Student class it belongs to. Now, Code-First will create only two foreign key column in Student table as shown below.

You can also use the Foreign Key attribute to include foreign key property with a different name as shown below: