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();
}
精彩评论