How to use EF Core when two properties in the Principle entity point to the same attribute in the dependent entity?

Uchchay Dugal 21 Reputation points
2021-11-01T04:16:37.687+00:00

I have 2 Model classes:


Department.cs

cs
public class Department
{
    public string DepartmentId { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [ForeignKey("FacultyId")]
    public IList<Faculty> Faculties{ get; set; }

    [ForeignKey("FacultyId")]
    public Faculty HOD { get; set; }

}

Faculty.cs

cs
public class Faculty
{
    public string FacultyId { get; set; }

    public string FacultyName { get; set; }

    public FacultyStatus FacultyStatus { get; set; }

    public Designation Designation { 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 Department{ get; set; }

}

I am having trouble determining how to set the HOD for the department. What I thought of was, making the HOD column a foreign key, but I don't know how good or useful that would be.
I referred this link to learn, but I am unable to apply the same


Error:

Unable to determine the relationship represented by navigation 'Department.Faculties' of type 'ICollection<Faculty>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
751 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,618 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,451 Reputation points Microsoft Vendor
    2021-11-02T02:51:55.7+00:00

    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:

    145587-image.png

    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:

    146115-image.png


    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

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Hazrat Ali 0 Reputation points
    2024-07-24T21:30:32.9566667+00:00

    This worked for me perfectly!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.