I'm trying to access the Item
navigation property of my OrderDetail
. But when I'm trying to run it, I get the InvalidOperationException
with the following error: There is already an open DataReader associated with this Command which must be closed first.
. The weird thing is when I'm running a step-by-step debug, everything is fine, I can see the items and the page is rendered with no problem whatsoever if and only if I'm looking into the orderDetails
down to the specific OrderDetail
prior to getting the item from the orderDetails
collection via foreach
.
var orderDetails = from o in db.Orders
join od in db.OrderDetails
on o.Id equals od.OrderId
where o.CustomerId == CustomerId
select od;
var availableItems = new List<ViewModel.Data.SelectItemEditorViewModel>();
foreach (var od in orderDetails)
{
var editorViewModel = new ViewModel.Data.SelectItemEditorViewModel();
editorViewModel.Selected = false;
editorViewModel.BaseItem = od.Item; //Exception is thrown when trying to access od.Item., but if I expand orderDetails in debug mode down to the specific orderDetail before entering the foreach loop, everything goes fine.
availableItems.Add(editorViewModel);
}
I've trying to add an Include(so the second line of LINQ would be join od in db.OrderDetails.Include(od => od.Item)
), but with no luck. What am I doing wrong?