实体框架关系映射

时间:2017-01-09 14:33:43

标签: entity-framework

我有以下两个类:

public class Department
{
    public int DepartmentID { get; set; }    
    public string Name { get; set; }    
    public int EmployeeCount { get; set; }    
    public int MinimumCoverage { get; set; }        
    public virtual List<Approver> Approvers { get; set; } 
    public virtual List<Employee> Employees { get; set; }
}

public class Employee
{
    public int EmployeeID { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }  
    public int DepartmentID { get; set; }
    public virtual Department Department { get; set; }
    public DateTime StartDate { get; set; }                
    public decimal DaysAccrued { get; set; }
    public decimal DaysUsed { get; set; }
    public decimal DaysRemaining { get; set; }  
    public bool IsManager { get; set; }
    public bool IsApprover { get; set; }
}

我希望员工属于使用DepartmentID的部门,而部门类中有员工列出部门中的所有员工,但我的EF设置中出现以下错误:

其他信息:'HolidayManagerContext.Employees'中的实体参与'Employee_Department'关系。找到0个相关的'Employee_Department_Target'。 1'Employee_Department_Target'

我的背景如下:

public partial class HolidayManagerContext : DbContext
    {
        public HolidayManagerContext()
            : base("name=HolidayManagerContext")
        {
        }

        public virtual DbSet<Employee> Employees { get; set; }
        public virtual DbSet<Approver> Approvers { get; set; }
        public virtual DbSet<Request> Requests { get; set; }
        public virtual DbSet<Decision> Decisions { get; set; }
        public virtual DbSet<Department> Departments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Employee>()
                .Property(e => e.DaysAccrued)
                .HasPrecision(18, 1);

            modelBuilder.Entity<Employee>()
                .Property(e => e.DaysUsed)
                .HasPrecision(18, 1);

            modelBuilder.Entity<Employee>()
                .Property(e => e.DaysRemaining)
                .HasPrecision(18, 1);

            modelBuilder.Entity<Employee>()
                .HasRequired(e => e.Department)
                .WithMany()
                .WillCascadeOnDelete(false);

        }
    }

2 个答案:

答案 0 :(得分:2)

流畅的配置

modelBuilder.Entity<Employee>()
    .HasRequired(e => e.Department)
    .WithMany()
    .WillCascadeOnDelete(false);

与模型导航属性(1)和FK字段(2)不匹配,导致您遇到的问题。

应该是这样的:

modelBuilder.Entity<Employee>()
    .HasRequired(e => e.Department)
    .WithMany(d => d.Employees) // (1)
    .HasForeignKey(e => e.DepartmentID) // (2)
    .WillCascadeOnDelete(false);

请注意,EF6默认FK名称约定为PropertyName_Id(在您的情况下为Department_Id)。

答案 1 :(得分:1)

使用代码优先数据注释您可以通过编辑模型来解决问题,它应该如下所示:

public class Department
{
    public Department()
    {
        Approvers = new List<Approver>();
        Employees = new List<Employee>();
    }
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public int EmployeeCount { get; set; }
    public int MinimumCoverage { get; set; }


    public virtual ICollection<Approver> Approvers { get; set; }
    public virtual ICollection<Employee> Employees { get; set; }
}

public class Employee
{

    public int EmployeeId { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime StartDate { get; set; }
    public decimal DaysAccrued { get; set; }
    public decimal DaysUsed { get; set; }
    public decimal DaysRemaining { get; set; }
    public bool IsManager { get; set; }
    public bool IsApprover { get; set; }

    public int DepartmentId { get; set; }

    [ForeignKey("DepartmentId")]
    public virtual Department Department { get; set; }
}

如果没有EF conventions,则需要在设置FK

的属性上使用ForeignKey属性
 public class Employee
  {
        ...
        public int DepartmentID { get; set; }

        [ForeignKey("DepartmentID")]
        public virtual Department Department { get; set; }
  }