简体   繁体   English

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

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

I am using entity framework and I have an Employee entity among other entities. 我正在使用实体框架,并且在其他实体中有一个Employee实体。 One navigation property that I want Employee to have is a list of Employees that this Employee manages. 我希望员工拥有的一个导航属性是该员工管理的员工列表。 This is the Employee class (it actually uses an EF prefix, but I'm referring to it without the prefix to make it less confusing): 这是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;
    }
}

As you can see, one of Employee's navigation properties is 如您所见,Employee的导航属性之一是

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

I have a an EmployeeMap class that uses Fluent API to establish the relationships between the Employee entity and other entities. 我有一个EmployeeMap类,该类使用Fluent API在Employee实体和其他实体之间建立关系。 It's incomplete right now, but this is what it looks like: 现在还不完整,但是看起来是这样的:

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);
    }
}

The first relation with the office and employees works and makes sense, since there are many Employee entities for each Office entity. 与办公室和雇员的第一个关系是有效的,因为每个办公室实体都有许多雇员实体。 However, I'm getting an error for the second relation mapping. 但是,第二个关系映射出现错误。 Specifically, the 'Employees' at the end gives an error saying 具体来说,最后的“雇员”给出了错误提示

Employee does not contain a definition for 'Employees' and no extension method etc... 员工不包含“员工”的定义,也没有扩展方法等。

It's pretty obvious why I get that error. 很明显为什么我会收到该错误。 I haven't declared a property in Employee for Employees and I don't know why I would. 我尚未在Employee forEmployees中声明财产,也不知道为什么要这么做。 But then how do I set up relationships between two entities of the same type? 但是,如何在两个相同类型的实体之间建立关系? If you look back at my Employee entity class, I also have the navigation property: 如果回头看一下我的Employee实体类,我还将具有导航属性:

public virtual EFEmployee ReportingTo { get; set; }

Here I'm also trying to set up a relation between an Employee and another Employee, but I don't know how that would work since in an entity data model the Employee entity is just represented as one table with its properties. 在这里,我还试图在Employee和另一名Employee之间建立关系,但是我不知道该如何工作,因为在实体数据模型中,Employee实体只是表示为具有其属性的一个表。 Does anyone know what I'm talking about and how to get around it? 有谁知道我在说什么以及如何解决它?

When you're defining relationships, the properties you use need to be the inverse of each other. 定义关系时,所使用的属性必须彼此相反。 In the case of Managing , I think what you're looking for is: Managing ,我认为您正在寻找的是:

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

Because I'm guessing you want: 因为我猜你想要:

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

and you want each EFEmployee in someManager.Managing to have ReportingTo == someManager . 你希望每个EFEmployeesomeManager.ManagingReportingTo == someManager


If you don't actually have an inverse property defined, you don't need to have one in the model configuration: 如果您实际上没有定义逆属性,那么在模型配置中就不需要一个逆属性:

// 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.

相关问题 使用流畅的API在旧数据库中以一对一的关系设置复合外键 - Using fluent API to set up composite foreign key in one-to-one relation in legacy database 如何添加员工 ID 并为员工创建集合和列表? - how do I add employee ID and create collection and list for employees? 如何为员工表中的记录分配唯一标识符? - How do i assign unique identifier for a record in employee table? 如何在Entity Framework中使用流利的API为一对一..one关系指定外键属性 - How to specify foreign key property for one to zero..one relation using fluent api in Entity Framework 我如何纠正&#39;qual.employee_id =人才招聘.employee_id中的错误“无法将类型&#39;int&#39;隐式转换为&#39;string&#39;”; &#39;? - How do I correct the error “cannot implicitly convert type 'int' to 'string'” in ' qual.employee_id = recruitment.employee_id; '? 如何解析员工的域和ID与之间的“ \\”? - How to parse employee's domain and Id with “\” in between? 如何使用流利的API配置两个表之间具有不同多样性的导航属性 - How do I configure navigation properties of varying multiplicity between two tables using fluent API 如何从Fluent API中为外键设置onUpdate? - How do I set onUpdate for a foreign key from the Fluent API? 在使用Windent API的Windsor安装程序中,如何将通用参数设置为仅特定类型的参数? - How do I set the generic parameter to only specific type parameters in this Windsor installer using the fluent API? 如何使用.net Web API从员工列表中获取最新的员工ID(字符串) - How can I get latest employee ID (string) from the list of employees using .net web api Get call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM