Foreign Key In Code First

Forums .NET CoreForeign Key In Code First
Staff asked 3 years ago

Which is best approach to apply foreign key?

Answers (1)

Add Answer
Mustakim Khalifa Marked As Accepted
Staff answered 3 years ago

The best way to apply Foreign Key in Code First is,

public class Employee {
    public int EmployeeId{ get; set; }
    public string EmployeeName { get; set; }
        
    [ForeignKey("Department")]
    public int DeptId{ get; set; }
    public Department Department { get; set; }
}

public class Department
{
    public int DepartmentID{ get; set; }
    public string DepartmentName{ get; set; }
    
    public ICollection<Employee> Employee  { get; set; }
}

I hope this will help you.

Subscribe

Select Categories