我正在尝试通过 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
为了使延迟加载工作,all类上的属性应定义为虚拟的。这是实体框架创建支持延迟加载的代理对象所必需的。
See here了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)