0

I have a department entity:

    public class Department : AuditableBaseEntity
    {
        public Company Company { get; set; }
        public Field Field { get; set; }
        public Employee Employee { get; set; } // Department Manager
        public string Description { get; set; }
        public Department ParentDepartment { get; set; }
        public virtual ICollection<Employee> Employees { get; set; } // Other employees of the department
        public virtual ICollection<DepartmentSurvey> DepartmentSurveys { get; set; }
        public virtual ICollection<OvertimeAttendance> OvertimeAttendances { get; set; }
        public virtual ICollection<WorkAttendance> WorkAttendances { get; set; }
        public virtual ICollection<Job> Jobs { get; set; }
        public virtual ICollection<Event> Events { get; set; }
        public virtual ICollection<Project> Projects { get; set; }
        public virtual ICollection<DepartmentTask> DepartmentTasks { get; set; }
    }

When i try to Add-Migration this error occurs: Unable to determine the relationship represented by navigation property 'Department.Employee' of type 'Employee'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

Which is the best practice in my case? Creating a mid table named EmployeeJob or JobEmployee and adding a role enum in this mid table, and then defining the manager in there, then delete the
public Employee Employee { get; set; } // Department Manager
line

and change line

public virtual ICollection<Employee> Employees { get; set; }

to this:

public virtual ICollection<EmployeeJob> EmployeeJob{ get; set; }

or leave all as it is and configure it in somewhere else (i don't know .net core much so where to configure this) ?

  • There is a mapping file between your database and the c# classes. The best way of updating the mapping is to add the tables/columns to the database and then refresh the mapping which will automatically update the classes to match the changes in the database. – jdweng Mar 23 '22 at 14:06
  • @jdweng any documentation? –  Mar 23 '22 at 14:13
  • See following : https://stackoverflow.com/questions/2947511/entity-framework-how-do-you-refresh-the-model-when-the-db-changes?force_isolation=true – jdweng Mar 23 '22 at 14:16
  • Post the Employee. You have introduced two separate relationships between Employee and Department, so you will need to configure them on both entities. – David Browne - Microsoft Mar 23 '22 at 14:35

0 Answers0