2017-01-09 231 views
2

我有以下兩類:實體框架映射關係

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

     } 
    } 
+0

Code First?任何流利的配置? –

+0

添加上下文也 –

+0

@IvanStoev問題更新 – Jay

回答

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_IdDepartment_Id你的情況)。

+1

感謝伊萬爲您解釋它是不勝感激 – Jay

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你需要使用ForeignKey的你在哪裏設置FK

一個屬性特性
public class Employee 
    { 
     ... 
     public int DepartmentID { get; set; } 

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