我正在尝试映射 EF fluent API 中似乎非常常见的情况并且已经碰壁了。我有以下课程:
public class Company
{
public int Id { get; set; }
public virtual List<Division> Divisions { get; set; }
public virtual List<Employee> Employees { get; set; }
}
public class Division
{
public int Id { get; set; }
public virtual List<Employee> Employees { get; set; }
public virtual Company Company { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Division Division {get; set;}
}
使用下表:
公司
ID - int
部门:
Id - int CompanyId int (FK)
员工
ID - int
姓名 - varchar(50)
DivisionId - int (FK)
如果 Employee 表有一个 CompanyID FK,这个映射将非常简单:
HasMany(c=>c.Employees).WithRequired().HasForeignKey(e=>e.CompanyId);
但是,由于我没有从 Employee 表到 Company 表的直接 FK,我似乎无法映射 Company 对象中的 Employees 属性以进行延迟加载。
我错过了什么?