我正在尝试通过Code First和EF 5.0加载导航属性。子对象加载为null。以下是代码。
[Table("ls_roles")]
public class Role
{
[Required]
[Key]
public int RoleID { get; set; }
[Required]
public String BarColor { get; set; }
[ForeignKey("RoleId")]
public virtual ICollection<ScheduleEmployee> Employees { get; set; }
}
[Table("ls_ScheduleEmployee")]
public class ScheduleEmployee
{
[Key]
[Required]
public int Id { get; set; }
[Required]
public int RoleId { get; set; }
[ForeignKey("RoleId")]
public Role Role { get; set; }
}
编辑:调用代码
class Program
{
static void Main(string[] args)
{
var x = new Model.ContextEntityFramework().ScheduleEmployees.FirstOrDefault();
}
}
此时 x.Role 为 null
Role
还是Employees
? - Steven V