4

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.

Iain Galloway
  • 16,882
  • 4
  • 50
  • 73
user472292
  • 969
  • 1
  • 19
  • 33

2 Answers2

9

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 Create method of DbSet (instead of instantiating the object with new):

Child c = context.Child.Create();

With active lazy loading this will create a proxy object which ensures that the navigation property gets loaded.

Slauma
  • 167,754
  • 56
  • 385
  • 407
  • This explained why I couldn't navigate into any parent navigational properties when everything else I was reading suggested it should just work. Thanks. – AdamH Apr 19 '12 at 22:23
  • @Slauma This is great, thank you. But how do I use the Create() method if my Child object has a constructor with required values? e.g. normally I would do Child c = new Child(OtherObject o). Obviously context.Child.Create(o) doesn't work. – Jordan Jul 08 '13 at 17:30
  • @Jordan: It's not possible. `Create` can only instantiate an entity via a default constructor. You must set entity properties after calling `Create` (or call something like an `Init` method which you pass the parameters into). – Slauma Jul 08 '13 at 17:57
0

A bit late after the question but just to clarify. As MS docs states here, I use this instead:

using (var context = new BloggingContext())
{
    var post = context.Posts.Find(2);

    // Load the blog related to a given post.
    context.Entry(post).Reference(p => p.Blog).Load();

    // Load the blog related to a given post using a string.
    context.Entry(post).Reference("Blog").Load();

    var blog = context.Blogs.Find(1);

    // Load the posts related to a given blog.
    context.Entry(blog).Collection(p => p.Posts).Load();

    // Load the posts related to a given blog
    // using a string to specify the relationship.
    context.Entry(blog).Collection("Posts").Load();
}

But it will work only if you use the Add() method

context.Set<T>().Add(post);
context.SaveChanges();
context.Entry(post).Reference(p => p.Blog).Load();

it will not work with .AddRange()

LeonardoX
  • 688
  • 8
  • 24