我正在使用实体框架,我在其他实体中有一个Employee实体。我希望Employee拥有的一个导航属性是此Employee管理的Employees列表。这是Employee类(它实际上使用了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;
}
}
如您所见,员工的导航属性之一是
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);
}
}
与办公室和员工的第一个关系是有效的,因为每个Office实体都有许多Employee实体。但是,我收到了第二个关系映射的错误。具体来说,&#39;员工&#39;最后给出了一个错误说
员工不包含&#39;员工&#39;的定义。没有扩展方法等......
很明显,为什么我会收到这个错误。我还没有在Employee for Employees中宣布财产,我也不知道为什么会这样做。但是,如何在同一类型的两个实体之间建立关系?如果你回顾我的Employee实体类,我也有导航属性:
public virtual EFEmployee ReportingTo { get; set; }
我在这里也试图建立一个员工与另一个员工之间的关系,但我不知道这是如何工作的,因为在实体数据模型中,Employee实体只表示为一个表与其属性。有谁知道我在谈论什么以及如何绕过它?
答案 0 :(得分:1)
当您定义关系时,您使用的属性需要彼此相反。在Managing
的情况下,我认为您正在寻找的是:
HasMany(t => t.Managing).WithRequired(t => t.ReportingTo);
因为我猜你想要:
someEmployee.ReportingTo.Managing.Contains(someEmployee) == true
并且您希望EFEmployee
中的每个someManager.Managing
都拥有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();