In this tutorial learn to use the ForeignKey attribute in Entity Framework to configure the Foreign Key Property. We use the Foreign Key to define the relationship between two tables in the database. For Example, the Employee
working in a Department
is a relationship. We express this relationship by creating a DepartmentID
field in the Employee
table and marking it as Foreign Key. In this example, the Department
is the Principal entity & Employee
is a Dependent entity.
Table of Contents
Foreign Key Default Convention
In the following model, the Entity Employee
has a Department
navigational property that links it to the Department
entity. We do not have any Property representing the Foreign Key field in Employee
entity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class Employee { public int EmployeeID { get; set; } public string EmployeeName { get; set; } //Navigation property public Department Department { get; set; } } public class Department { public int DepartmentID { get; set; } public string DepartmentName { get; set; } //Navigation property public ICollection<Employee> Employeees { get; set; } } |
The Entity Framework conventions will create the Foreign Key field in the database for us. It will use the name NavigationpropertyName_PrimaryKeyOftheNavigationPropertyType
.
But, if you add the DepartmentID
property in the Employee
entity, Entity Framework conventions will use that field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Employee { public int EmployeeID { get; set; } public string EmployeeName { get; set; } public int DepartmentID { get; set; } //Navigation property public Department Department { get; set; } } public class Department { public int DepartmentID { get; set; } public string DepartmentName { get; set; } //Navigation property public ICollection<Employee> Employeees { get; set; } } |
ForeignKey Attribute
What if we wish to change the DepartmentID
Property to DeptID
in the Employee table. The Default Convention in EF will not recognize the DeptID
Property and will create Department_DepartmentID
column in the database.
We can override this behavior using the Foreign key attribute on the navigational property. The following is the syntax of the ForeignKey Attribute.
1 2 3 4 5 6 | ForeignKey(string name) Where Name: The name of the associated navigation property or of the associated foreign keys |
There are three ways you can apply this attribute
- ForeignKey property of the dependent class
- Navigational Property of the dependent class
- Navigational Property of the Principal class
In the example above example, Employee is the dependent class as it depends on the Department. The Department is the Principal class.
Foreign Key property of the dependent class
The following example, we apply the ForeignKey attribute on the DeptID
Property of the Employee class. In that case, the name must point to the navigational property
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Employee { public int EmployeeID { get; set; } public string EmployeeName { get; set; } [ForeignKey("Department")] public int DeptID { get; set; } //Navigation property public Department Department { get; set; } } public class Department { public int DepartmentID { get; set; } public string DepartmentName { get; set; } //Navigation property public ICollection<Employee> Employeees { get; set; } } |
We can also place the ForeignKey Attribute Navigation property. When placed on navigation property, it should specify the associated foreign key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Employee { public int EmployeeID { get; set; } public string EmployeeName { get; set; } public int DeptID { get; set; } //Navigation property [ForeignKey("DeptID")] public Department Department { get; set; } } public class Department { public int DepartmentID { get; set; } public string DepartmentName { get; set; } //Navigation property public ICollection<Employee> Employeees { get; set; } } |
We can also place the Foreign key attribute on the Navigational property of the Principal class. The name argument must point to the Foreign Key of the dependent class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Employee { public int EmployeeID { get; set; } public string EmployeeName { get; set; } public int DeptID { get; set; } //Navigation property public Department Department { get; set; } } public class Department { public int DepartmentID { get; set; } public string DepartmentName { get; set; } //Navigation property [ForeignKey("DeptID")] public ICollection<Employee> Employeees { get; set; } } |
Reference
Read More
- Entity Framework Tutorial
- Configure the Entity Data Model
- Code First Conventions in Entity Framework
- Data Annotations in Entity Framework
- Key Attribute
- Fluent API in Entity Framework
- Relationships in Entity Framework
- One to one relationship in Entity Framework
- One to Many relationships in Entity Framework
- Many to Many relationships in Entity Framework
It helped me! Thanks!
I name all my Id simply Id. So from my point of view it seems EF6 do better than EF Core.