使用 EF 核心与对象的一对多关系

One to many relationship with objects using EF core

我有四个实体:

Employee:属于Group和Department实体 组:包含员工列表 部门:包含员工列表 待办事项:分配给一个组

问题是:我尝试将 FK 添加到表中,并且像其他 10 种方法一样,但我就是想不通。让我们看看部门:

public class Department
    {
        public Department()
        {
            Employees = new List<Employee>();
        }

        [Key]
        public int DepartmentId { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Field { get; set; }

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

这是员工实体:

public class Employee
    {
        public int EmployeeId { get; set; }

        [Required]
        [MaxLength(50, ErrorMessage = "Name cannot exceed 50 characters")]
        public string Name { get; set; }

        [Required]
        [RegularExpression(@"^[a-zA-Z0-9_0+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", ErrorMessage = "Invalid Email Format")]
        [Display(Name = "Office Email")]
        public string Email { get; set; }

        [ForeignKey("Department")]
        public int DepartmentId { get; set; }

        public Department Department { get; set; }

        [ForeignKey("Group")]
        public int GroupId { get; set; }

        public Group Group { get; set; }
    }

我为部门生成了一个简单的控制器和视图页面 在详细信息方法中,我尝试打印出 department.Employees.Count() 但它说它是 0.

这是我的 appDbContext:

modelBuilder.Entity<Employee>()
                .HasData(
                new Employee() { EmployeeId = -99, Name = "Mary", Email = "mary@gmail.com", DepartmentId = -99, GroupId = -1 },
                new Employee() { EmployeeId = -98, Name = "Stan", Email = "stan@gmail.com", DepartmentId = -99, GroupId = -1 },
                new Employee() { EmployeeId = -97, Name = "Mike", Email = "mike@gmail.com", DepartmentId = -99, GroupId = -1 });

            modelBuilder.Entity<Department>()
                .HasData(
                new Department() { DepartmentId = -99, Field = "IT", Name = "Programming Department" },
                new Department() { DepartmentId = -98, Field = "HR", Name = "Human Resorcues" },
                new Department() { DepartmentId = -97, Field = "AD", Name = "Advertisement Department" });

你的问题有很多“漏洞”,你必须继续努力和学习。因此,如果您需要更多帮助,将会更新此问题。

我认为您在理解方面有困难 objects。

如果你想在你的部门中这样做,部门有一份员工名单(正如我在你的陈述中看到的那样):

但是:

   public List<Employee> Employees 
    {
      get; set;
    }

然后:

 new Department() { DepartmentId = -99, Field = "IT", Name = "Programming Department", Employees = x },

其中 x 是一个实例或列表,或者是它本身:

modelBuilder.Entity<Employee>()
                .HasData(
                new Employee() { EmployeeId = -99, Name = "Mary", Email = "mary@gmail.com", DepartmentId = -99, GroupId = -1 },
                new Employee() { EmployeeId = -98, Name = "Stan", Email = "stan@gmail.com", DepartmentId = -99, GroupId = -1 },
                new Employee() { EmployeeId = -97, Name = "Mike", Email = "mike@gmail.com", DepartmentId = -99, GroupId = -1 })

但是对于数据库来说 E.F 更容易。:

//Save your employee in the context.
_context.Employees.Add(newemployee);
//Set the employee in the departament
thedepartament.Employees.Add(newemployee); 
//Save everything
_context.SaveChanges();

更新:

当然你不会每次“添加”一个新的“员工”时都创建一个部门,上面的例子只是为了同时插入一个关系child。所以你需要的是这样思考:

//Save your employee in the context.
_context.Employees.Add(newemployee);
//Search for the departament you want to add this employee, with a query:
Departament thedepOfTheNewEmployee = _context.Departament.Where(your conditions to search it).FirstOfDefault();
//Set the employee to that departament
newemployee.DepartmentId = thedepOfTheNewEmployee.Id;
//Save everything
_context.SaveChanges();

更进一步的理论:
如果您将元素添加到上下文中,将(虚拟地)提供一个要检索的 Id。这个理论不适合你的场景,但对你以后有帮助。