3

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

EDIT: CALLING CODE

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

x.Role == null at this point

5
  • What exactly is loading as null? Commented Mar 11, 2013 at 15:46
  • Which is the "child" property that is null? Role or Employees Commented Mar 11, 2013 at 15:46
  • the role is coming back as empty when you look at scheduleemployee object Commented Mar 11, 2013 at 15:47
  • if the role is required, then you shouldn't have to make it virtual. not sure if that is stopping it. probably not Commented Mar 11, 2013 at 15:50
  • i removed the virtual, still coming back as null Commented Mar 11, 2013 at 15:51

4 Answers 4

8

In order for lazy-loading to work, all properties on the class should be defined as virtual. This is required for Entity Framework to create a proxy-object that supports lazy-loading.

See here for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

I updated my answer with a link to the official documentation that lists all requirements for EF to generate proxies.
7

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

something like

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

3 Comments

Thank you for pointing me in the right direction. The code to make this work ended up being var z = new Model.ContextEntityFramework().ScheduleEmployees.Include("Role").FirstOrDefault();
The include causes the navigation property to be eagerly loaded, which isn't necessarily bad, but definitely not ideal for a lot of situations. The proper solution is to get lazy-loading working.
Lazy loading isnt an option in the current environment
1

Your Role class does not need to use the ForeignKey attribute at all on the Employees collection. EF will automatically know to do the mapping based off of the ScheduleEmployee object and its use of the ForeignKey attribute.

Comments

0
 [Table("ls_roles")] public class Role { [Required] [Key] public int RoleID { get; set; } [Required] public String BarColor { get; set; } public virtual ICollection<ScheduleEmployee> Employees { get; set; } } [Table("ls_ScheduleEmployee")] public class ScheduleEmployee { [Key] [Required] public int Id { get; set; } [Required] [ForeignKey("Role")] public int RoleId { get; set; } public virtual Role Role { get; set; } } 

1 Comment

Could you please add some description of your code, in particular how and why it solves the problem!?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.