【发布时间】:2015-06-11 17:54:01
【问题描述】:
我正在使用实体框架,并且我在其他实体中有一个 Employee 实体。我希望员工拥有的一个导航属性是该员工管理的员工列表。这是 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;
}
}
如您所见,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 不包含“Employees”的定义,也没有扩展方法等...
我得到这个错误的原因很明显。我还没有在 Employee for Employees 中声明财产,我不知道为什么要这样做。但是,我如何在相同类型的两个实体之间建立关系呢?如果你回头看看我的 Employee 实体类,我也有导航属性:
public virtual EFEmployee ReportingTo { get; set; }
在这里,我还尝试在 Employee 和另一个 Employee 之间建立关系,但我不知道这将如何工作,因为在实体数据模型中,Employee 实体仅表示为一个具有其属性的表。有谁知道我在说什么以及如何绕过它?
【问题讨论】:
标签: c# entity-framework mapping ef-fluent-api