In this tutorial learn to use the ForeignKey Attribute in Entity Framework Core to configure the Foreign Key Property. We use the Foreign Key to define the relationship between 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. The Entity framework Core automatically creates the Foreign Key field in the database for us.
the Entity Framework Core conventions will use the name of the Primary key field of the Principal class for the Foreign Key field
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; } } |
But, if you add the DepartmentID
property in the Employee
entity, Entity Framework Core conventions will make use of that field and will not create another 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 Core will not recognize the DeptID
Property and will create 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