I am in the process of porting an 'old' EF project to EF 6.1 Code First. The project uses TPH (table per hierarchie) to store customers. A Customer is a subclass of User and a Order has a customer. A customer belongs to a specific subshop. Like this in UML:
I have mapped the customer with a discriminator column like this:
modelBuilder.Entity<User>()
.Map<Customer>(m => m.Requires("IsNormalCustomer").HasValue(1))
.Map<Printer>(m => m.Requires("IsNormalCustomer").HasValue(2));
So all Customers have a IsNormalCustomer value of '1'. A Printer is also a user but not a customer.
In my order repository I have same code to find all orders of a specific subshop.
orders = GetPendingOrders().Where(o => o.Customer.SubShop.SubShopId == subshopId);
If I don't filter on subshop the query runs fine but with filtering I get the following exception:
System.Data.Entity.Core.EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details. ---> System.Data.Entity.Core.MetadataException: The declared type of navigation property Fotogoed.Data.Order.Customer is not compatible with the result of the specified navigation. at System.Data.Entity.Core.Query.PlanCompiler.PreProcessor.ValidateNavPropertyOp(PropertyOp op)
Could anyone shed some light on what could be the cause of this error? Feel free to ask more information if needed.
I'll answer my own question here for people running in to the same problem. The problem was I addded a custom mapping for user->customer but forgot to push down the orders list to the customer class (this was in the user). Fixing that mapping resolved the issue.