Hi @Uchchay Dugal ,
From your code, you want to configure two entities (Department and Faculty) with more than one relationship: One-to-One and One-to-Many. If that is the case, you could consider using the InverseProperty
attribute.
For example:
public class Department
{
public string DepartmentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[InverseProperty("FacultyDepartment")]
public IList<Faculty> Faculties { get; set; } //navigation property, One-to-Many relationship
[InverseProperty("HODDepartment")]
public Faculty HOD { get; set; } //navigation property, One-to-One relationship
}
public class Faculty
{
[ForeignKey("HODDepartment")]
public string FacultyId { get; set; } // this property is the primary key, and the one-to-one relationship foreign key. You can also add a new Primary key.
public string FacultyName { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }
public string Address { get; set; }
public int Experience { get; set; }
public string Qualifications { get; set; }
public string Description { get; set; }
public Department HODDepartment { get; set; } // one-to-one relationship, navigation property.
[ForeignKey("FacultyDepartment")]
public string FacultyDepartmentId { get; set; } //one-to-many relationship foreign key.
public Department FacultyDepartment { get; set; } //one to-many relationship , navigation property.
}
Then, after migration, the result as below:
Update:
HOD (this should be the Id of the Faculty who is the HOD)
To add the HodId in the DepartMent table, based on the previous sample code, you can add a foreign key in the Departments class:
public class Department
{
public string DepartmentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[InverseProperty("FacultyDepartment")]
public IList<Faculty> Faculties { get; set; }
[ForeignKey("HOD")]
public string HODId { get; set; }
public Faculty HOD { get; set; }
}
The Faculty class as below:
public class Faculty
{
[ForeignKey("HODDepartment")]
public string FacultyId { get; set; }
public string FacultyName { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }
public string Address { get; set; }
public int Experience { get; set; }
public string Qualifications { get; set; }
public string Description { get; set; }
public Department HODDepartment { get; set; }
[ForeignKey("FacultyDepartment")]
public string FacultyDepartmentId { get; set; }
public Department FacultyDepartment { get; set; }
}
Then, after migration, the table as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion