我有以下兩類:實體框架映射關係
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);
}
}
Code First?任何流利的配置? –
添加上下文也 –
@IvanStoev問題更新 – Jay