【发布时间】:2017-01-09 14:33:43
【问题描述】:
我有以下两个类:
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);
}
}
【问题讨论】:
-
代码优先?任何 Fluent 配置?
-
@IvanStoev 问题已更新
标签: entity-framework