我想通过Code First和EF 5.0加载导航属性子对象加载为null。下面是代码。Code First和EF 5.0无法加载导航属性
[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在这一点上
什么是加载为null? – IronMan84 2013-03-11 15:46:20
哪个是“孩子”属性是null? 'Role' or'Employees' – 2013-03-11 15:46:55
如果角色是必需的,那么当您查看scheduleemployee对象 – gh9 2013-03-11 15:47:48