I'm having huge difficulties getting my navigation properties to work in EF Code First. As an abstracted example, I have:
public class Parent{ public int ParentID {get; set;} public virtual List<NamedChild> Children {get; set;} public Parent(){} public void Init(int ParentID, List<UnnamedChild> Children){ this.ParentID = ParentID; this.Children = Children.ConvertAll(x => new NamedChild(x, "")); } } public class NamedChild{ public int ChildID {get; set;} public string Name {get; set;} public NamedChild(UnnamedChild c, string Name){ this.ChildID = c.ChildID; this.Name = Name; } } public class UnnamedChild{ public int ChildID {get; set;} public UnnamedChild(int ChildID){ this.ChildID = ChildID; } } and then later...
List<UnnamedChild> children = GetChildrenFromSomewhere(); Parent p = db.Parents.Create(); p.Init(1, children); db.Parents.Add(p); db.SaveChanges(); Now if I'm debugging I can look into the current DbSet and it shows that there is 1 Parent, and its "Children" property is set to a List of 2 NamedChild. This is good, this is what it should be. However, if I stop the program and re-run it, when I look in the DbSet there is still 1 Parent, but its "Children" property has been set to null.
In summary, immediately after saving it the values are right, but as soon as I re-load the DB Context those values are missing (nulls). I am running the most recent EF with LazyLoading enabled.
It should be noted that if I use .Include(), it will populate those null values with the proper NamedChild list, but I need this to work with LazyLoading.