如何使用 Fluent API 在 Employee 实体和 ICollection<Employee> 之间建立关系?

How do I set up the relation using Fluent API between an Employee entity and an ICollection<Employee>?

我正在使用 entity framework 并且我在其他实体中有一个 Employee 实体。我希望 Employee 拥有的一个导航 属性 是该 Employee 管理的 Employees 列表。这是 Employee class(它实际上使用了 EF 前缀,但我指的是没有前缀以减少混淆):

/// <summary>
/// The Class model for the Employee Entity
/// </summary>
[Table("employee_entity")]
public class EFEmployee : EFBusinessEntity
{
    /// <summary>
    /// The hire date of the employee
    /// </summary>
    [Column("hire_date")]
    public DateTime HireDate { get; set; }

    /// <summary>
    /// The list of jobtitles for the employee
    /// </summary>
    [Column("job_title")]
    [Required]
    public byte[] JobTitle { get; set; }

    /// <summary>
    /// The Employee's salary (note: not attached to jobtitle necessarily)
    /// </summary>
    [Column("salary")]
    public int Salary { get; set; }

    /// <summary>
    /// List of Certifications for the employee
    /// </summary>
    [Column("certifications")]
    public byte[] Certifications { get; set; }

    /// <summary>
    /// Employee's stored up vacation time
    /// </summary>
    [Column("vacation_time")]
    public int VacationTime { get; set; }

    /// <summary>
    /// Employee's stored up sick time
    /// </summary>
    [Column("sick_time")]
    public int SickTime { get; set; }

    /// <summary>
    /// Maps the employee entity to the office entity
    /// </summary>
    [ForeignKey("office_entity")]
    public virtual EFOffice Office { get; set; }

    /// <summary>
    /// Maps the employee entity to the person entity
    /// </summary>
    public virtual EFPerson Identity { get; set; }

    /// <summary>
    /// Maps the employee entity to another employee entity
    /// </summary>
    public virtual EFEmployee ReportingTo { get; set; }

    /// <summary>
    /// Maps the employee entity to a collection of employee entites
    /// </summary>
    public virtual ICollection<EFEmployee> Managing { get; set; }

    /// <summary>
    /// Constructor for an Entity. Only requires the properties that cannot be null.
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="TenantId"></param>
    /// <param name="hire"></param>
    /// <param name="titles"></param>
    /// <param name="salary"></param>
    /// <param name="certifications"></param>
    /// <param name="vacationTime"></param>
    /// <param name="sickTime"></param>
    public EFEmployee(Guid Id, Guid TenantId, DateTime hire, byte[] titles, int salary, byte[] certifications, int vacationTime, int sickTime)
    {
        this.HireDate = hire;
        this.JobTitle = titles;
        this.Salary = salary;
        this.Certifications = certifications;
        this.SickTime = sickTime;
        this.VacationTime = vacationTime;
    }
}

如您所见,Employee 的导航属性之一是

public virtual ICollection<EFEmployee> Managing { get; set; }

我有一个 EmployeeMap class,它使用 Fluent API 来建立 Employee 实体和其他实体之间的关系。目前还不完整,但它看起来像这样:

public partial class EmployeeMap : EntityTypeConfiguration<EFEmployee>
{
    public EmployeeMap()
    {
        // Relations
        HasRequired(t => t.Office).WithMany(u => u.Employees);
        HasMany(t => t.Managing).WithRequired(u => u.Employees);
    }
}

与办公室和员工的第一个关系有效且有意义,因为每个办公室实体都有许多员工实体。但是,第二个关系映射出现错误。具体来说,最后的 'Employees' 给出了一个错误

Employee does not contain a definition for 'Employees' and no extension method etc...

很明显为什么会出现该错误。我还没有在 Employee 中为 Employees 声明 属性,我也不知道为什么要这样做。但是,如何在同一类型的两个实体之间建立关系呢?如果你回头看看我的 Employee 实体 class,我也有导航 属性:

public virtual EFEmployee ReportingTo { get; set; }

在这里,我还尝试在一名员工和另一名员工之间建立关系,但我不知道这将如何工作,因为在实体数据模型中,员工实体仅表示为一个 table 及其属性。有谁知道我在说什么以及如何绕过它?

当您定义关系时,您使用的属性需要彼此相反。对于Managing,我想你要找的是:

HasMany(t => t.Managing).WithRequired(t => t.ReportingTo);

因为我猜你想要:

someEmployee.ReportingTo.Managing.Contains(someEmployee) == true

并且您希望 someManager.Managing 中的每个 EFEmployee 都有 ReportingTo == someManager


如果您实际上没有定义逆属性,则您不需要在模型配置中有一个:

// someEmployee can possibly have many other EFEmployees with 
// ReportingTo == someEmployee, but no collection property on someEmployee to 
// represent that relationship!
HasRequired(t => t.ReportingTo).WithMany();