代码优先和EF 5.0不加载导航属性 [英] Code First and EF 5.0 not loading navigation properties

查看:795
本文介绍了代码优先和EF 5.0不加载导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过代码优先和EF 5.0的子对象加载为空加载导航属性。下面是代码。

I am trying to load a navigation property through Code First and EF 5.0 The child object is loading as null. below is the code.

  [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; }
    }



编辑:调用代码

CALLING CODE

class Program
{
    static void Main(string[] args)
    {
        var x = new Model.ContextEntityFramework().ScheduleEmployees.FirstOrDefault();
    }
}



x.Role == NULL在这一点

x.Role == null at this point

推荐答案

您必须做你的调用代码,包括儿童.INCLUDE。

You have to do a .include on your calling code to include children.

Model.ContextEntityFramework().ScheduleEmployees.Include(x => x.Role).FirstOrDefault();

这篇关于代码优先和EF 5.0不加载导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆