This LineItems property is a collection of LineItem objects like this and in this object, there's another navigation property called Product. Basically, this is a hierarchy. Think of a ProductType object inside Product class.

But instead, you get an error...The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
It's because, by default Entity Framework uses lazy-loading for navigation properties. That's why these properties are marked as virtual - entity framework creates proxy class for your entity and overrides navigation properties to allow lazy-loading. If you debug, you can see the returned objects from EF looks like "Orders_34AB....." something. It is the returned proxy inherited from this entity and has provided DbContext instance to this proxy in order to allow lazy loading of membership later. So, entity has instance of DbContext which was used for loading entity. That's your problem. You have using block around Ordersusage. Which disposes context before entities are returned. When some code later tries to use lazy-loaded navigation property, it fails, because context is disposed at that moment.

So, what you need to do is, pre-load all LineItems, Products, and ProductTypes and if there are any other navigation properties, all of them!

var query = context.Orders// All orders.IncludeMultiple(
c => c.LineItems// Include all line items for a order.Select(o => o.Product// each line item, include its product.Select(o => o.ProductType)));// each product, include its type

Popular posts from this blog

If you have ever tried to build a POS (Point Of Sales or Point of Services) application, you might have come across the requirement to print a sales receipt like the following. Not just retail stores, but these receipts are produced by banks, hospitality and healthcare too.

Most of the time, such a receipt can be printed using a POS printer, which looks like the one below. The application developer needs to send ESC/POS commands to such a printer (eg: EPSON) to print the actual receipt. Depending on the manufacturer the capabilities can be vary. However, most of them can handle most of the print commands and print a nicely formatted receipt.

The main advantage of using raw ESC/POS commands for printing is that the printing performance will be faster but this depends on the printer itself.

In this post, I will be using a windows application to send commands to the printer. If you want to do this through a ASP.NET website, the process is a bit more complex and I will try to create anot…

Recently (Not so long ago, more like 10 mins ago) my asked me to automatically redirect the user to the login screen after session is expired. I googled it, found 2 ways. One works well with NON-AJAX websites and it is the simplest.

If you are making a site like facebook, then using the above method would cause number of users to decrease from 1 billion to 0. Why? because this method counts only full page refreshes, not ajax requests.

I am accessing a 3rd party client WCF secured web service using a client certificate issued by them. I have added this client certificate to my servers' certificate store. (Ref#1 (packing a certificate), Ref#2 (importing to store))