This project has moved and is read-only. For the latest updates, please go here.

Fixed: cannot view orders after they are entered


I cloned naa4e [commit 1b0c379] and noticed that in IBuyStuff-CQRS I could create a new order but then 'manage orders' would throw an error and not display the order details (Details.OrderItem.Product is null when it should not be)

so basically the The demo of LET (discussed in the book) is not actually working in this cut of the solution, but I've foud how to fix it.

It turns out there were two separate problems.

I've found that adding AsNoTracking inside QueryModelDatabase and doing a ToList() before trying to do a projection fixed the issue.

1) One problem seems to be that the 'read-side' using EF is seeing that Order has a relationship to Customer and is assuming that the foreign key should be called Customer_CustomerId when in fact (from the Command-side model) the navigation property is actually called 'Buyer' but the read-side (QueryModelDatabase) does not know this and when tracking is enabled, EF is being too intrusive and trying to work out relationships that are not ultimately being used or needed in the 'read-side' model.

This does seem to be a bit of a bug in EF to me...? AsNoTracking seems to help EF to ignore Customer/Buyer entirely (as it should).

2) doing a projection the way it is done in OrderControllerService (ultimately reaching into Order.Items.Product) fails despite the Include(). I found quite a few blog posts on how Includes get ignored when doing certain kinds of projections. .ToList() is an ugly work-around but it works. The alternative is to also project through the hierarchy (i.e. do a SelectMany() into a child ViewModel). Specifically, OrderFoundViewModel.Details could perhaps be a custom ViewModel / DTO that is projected out with a SelectMany over order.Items). I've not tried this but it should work.

Hope this helps someone!