I try to learn the code inside the Entity Framework first, and I am having problems modeling relationships. This is the basic personnel database, which for this has two organizations: "Employees and Departments".
The employee belongs to the department, and the department has an administrator and group manager, both of whom are active employees. I tried to simulate this using the following:
EMPLOYEE public int? DepartmentID { get; set; } public virtual Department Department { get; set; } Context: modelBuilder.Entity<Employee>().HasOptional(x => x.Department); DEPARTMENT public class Department { [Required] public int DepartmentID { get; set; } [Required(ErrorMessage = "The description is required.")] public string Description { get; set; } public int? ManagerID { get; set; } public virtual Employee Manager { get; set; } public int? TeamAdministratorID { get; set; } public virtual Employee TeamAdministrator { get; set; } } Context: modelBuilder.Entity<Department>().HasOptional(x => x.Manager); modelBuilder.Entity<Department>().HasOptional(x => x.TeamAdministrator);
Obviously, I would like the department table to have only four columns - DepartmentID, Description, ManagerID and TeamAdministratorID, but two more are generated for these relationships, namely Manager_EmployeeID and Team_Administrator_EmployeeID. In addition, a Department_DepartmentID column is created in the Employee table to hold the DepartmentID instead, using the DepartmentID column specified in the entity.
What am I doing wrong? How do I define fields and relationships to prevent the code from first ignoring what I pointed out and generating its own navigation fields in the database?
Mc76
source share