I have a parent and child object. If I do the following
Child c = new Child();
c.ParentID = parentID;
context.Child.Add(c);
context.SaveChanges();
int i = c.Parent.ParentID; // throws an exception b/c Parent is null
Why is this doing this? If I get a new context (after saving), I can see Parent just fine.
I guess you are working with lazy loading enabled. If you want that the navigation property gets populated after adding the object with the foreign key property to the context you must use the
Createmethod ofDbSet(instead of instantiating the object withnew):With active lazy loading this will create a proxy object which ensures that the navigation property gets loaded.