繁体   English   中英

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

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

我正在使用实体框架,并且在其他实体中有一个Employee实体。 我希望员工拥有的一个导航属性是该员工管理的员工列表。 这是Employee类(它实际上使用EF前缀,但是我指的是不带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类,该类使用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);
    }
}

与办公室和雇员的第一个关系是有效的,因为每个办公室实体都有许多雇员实体。 但是,第二个关系映射出现错误。 具体来说,最后的“雇员”给出了错误提示

员工不包含“员工”的定义,也没有扩展方法等。

很明显为什么我会收到该错误。 我尚未在Employee forEmployees中声明财产,也不知道为什么要这么做。 但是,如何在两个相同类型的实体之间建立关系? 如果回头看一下我的Employee实体类,我还将具有导航属性:

public virtual EFEmployee ReportingTo { get; set; }

在这里,我还试图在Employee和另一名Employee之间建立关系,但是我不知道该如何工作,因为在实体数据模型中,Employee实体只是表示为具有其属性的一个表。 有谁知道我在说什么以及如何解决它?

定义关系时,所使用的属性必须彼此相反。 Managing ,我认为您正在寻找的是:

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

因为我猜你想要:

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

你希望每个EFEmployeesomeManager.ManagingReportingTo == 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM