开发者

Reloading navigation properties after detaching a new object

I am using Entity Framework with POCO objects and have the following scenario:

I create a new parent object and add a child object to it. Then I save changes and detach the parent object. At this moment its children collection gets empty.

parent = new Parent() { label = "Test" };
parent.Children.Add(new Child() { label = "Test" });
context.Parents.AddObject(parent);
context.SaveChanges();
context.Detach(parent);

When I reattach the parent object to a different context, I have to explicitly load the corresponding property to access the children collection.

context.Parents.Attach(parent);
context.LoadProperty(parent, p => p.Children);

Is there any way for the navigation properties to lazy load instead of having to load manually every one of them?

If 开发者_StackOverflowinstead of creating a new object, I retrieve an existing object, the problem does not occur: the children collection gets empty when detached; but after reattaching, the children are lazy loaded automatically.


Instead of:

parent = new Parent() { label = "Test" };

Try to use:

parent = context.CreateObject<Parent>();
parent.label = "Test";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜