开发者

Automatic Lazyloading on Entity Framework 4.0

Suppose I have an Entity Order with OrderDetails as child preperty.

I enable lazyloading like this:

  _context.ContextOptions.LazyLoadingEnabled = true;

I Can feed a view with a method like this:

Order.开发者_开发百科GetAll()

And navigate by the order details automatically without getting the wirerd "Object reference not set to an instance of an object" error??


If you have lazy loading, when you load up the objects you need to explicitly include the sub objects.

So Order.GetAll() will include

return context.Orders.Include("OrderDetails");

Another alternative is to load up the order details later, like so:

if (!order.OrderDetailsHeaders.IsLoaded)
{
    order.OrderDetailsHeaders.Load();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜