1

I have a couple of classes:

public class MyGoalsModel
{
    [Key]
    public string Name { get; set; }

    /*Some local bools*/

    public List<MyGoalString> myGoals { get; set; }
}

public class MyGoalString
{
    public int MyGoalStringID { get; set; }

    public string GoalString { get; set; }
    public bool Selected { get; set; }
}

I can populate them correctly, and the code (EF?) generates the necessary hidden foreign keys to link them (all ok in SQL) and recover the information for MyGoalsModel, but the List is always null.

I use the following to get the entry I want:

 MyGoalsModel goals = db.MyGoals.Find(Name);

but when I investigate the code goals.MyGoals is always null.

Am I missing something, is there a better way to recover the information with the lists present?

Shocklanced
  • 153
  • 1
  • 1
  • 8

1 Answers1

2

Add the keyword virtual so EF can create a proxy for your List and lazy load the data when needed.

Edit: Or as stated in the accepted answer in this question.

Community
  • 1
  • 1
cverb
  • 645
  • 6
  • 20