【发布时间】:2011-12-12 23:35:31
【问题描述】:
我正在尝试映射 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 - 整数
部门:
ID - 整数
CompanyId int (FK)
员工
ID - 整数
名称 - varchar(50)
DivisionId - int (FK)
如果 Employee 表有一个 CompanyID FK,这个映射将非常简单:
HasMany(c=>c.Employees).WithRequired().HasForeignKey(e=>e.CompanyId);
但是,由于我没有从 Employee 表到 Company 表的直接 FK,我似乎无法映射 Company 对象中的 Employees 属性以进行延迟加载。
我错过了什么?
【问题讨论】:
标签: .net entity-framework fluent database-first ef-database-first