When using Entity Framework Code First approach, there are actually three ways to define foreign keys. In this post let’s explorer these different ways with some examples (Please note here I am not going to cover the basics of EF, assuming you have some knowledge in Entity Framework.) . Before starting off with the code, first let’s have a quick look at the mentioned three different ways.
First way is pretty much straight forward. That is when you add a navigation property, EF it self will create a foreign key relationship. The second one is using Data Annotations. The last would be using Fluent API.
Now let's see all these in action. Please consider the following two entity classes.
Here I have two classes which are “Department” and “Employee”. In “Employee” class, I have a navigation property named “Department” which is of type “Department”. So here what I says is Employee is belonging to a Department.
I have the following “MyContext” class which inherits from DbContext.
There in my overrided OnModelCreating method, I am saying that I don’t want to pluralize the table names when tables are created in EF. (You can basically ignore that particular line of code, as it has nothing to with the topic today).
Now in my Main method, I am writing the following code to add some data to “Department” table, so the tables will get generated.Once I run the code and check the database created, I can see the following.
|Through Navigation Property|
In the “Employee” table, you can see there is a foreign key created named “Department_DepartmentId”. So that is the first way.
Now let’s have a look at defining the foreign key using Data Annotations.
For that, I am going to modify the “Employee” class as follows.
Here I have defined a property of type int named “DepartmentId”. And I have annotated my “Department” property saying that Foreign key is newly added “DepartmentId”. I am deleting the previous database and running the program back again. Now I can see the following database structure.
|Through Data Annotations|
In “MyContext” class I am modifying the OnModelCreating() method as follows and I am removing my Data Annotations in the "Department" class.
Again I am deleting the previous database and running the program back again. When I examine the created the database now, It’s as same as the previous method.
|Through Fluent API|
So hope you find this post helpful. I am uploading the sample to my OneDrive. Do check it out and appreciate your feedback.