【问题标题】:The declared type of navigation property xxx is not compatible with the result of the specified navigation导航属性 xxx 声明的类型与指定导航的结果不兼容
【发布时间】:2015-02-23 11:29:54
【问题描述】:

我正在将“旧”EF 项目移植到 EF 6.1 Code First。该项目使用 TPH(每个层次结构的表)来存储客户。客户是用户的子类,订单有客户。客户属于特定的子商店。在 UML 中是这样的:

我已将客户映射到这样的鉴别器列:

   modelBuilder.Entity<User>()
            .Map<Customer>(m => m.Requires("IsNormalCustomer").HasValue(1))
            .Map<Printer>(m => m.Requires("IsNormalCustomer").HasValue(2));

因此,所有客户的 IsNormalCustomer 值都为“1”。打印机也是用户,但不是客户。

在我的订单存储库中,我有相同的代码来查找特定子商店的所有订单。

orders = GetPendingOrders().Where(o => o.Customer.SubShop.SubShopId == subshopId);

如果我不对 subshop 进行过滤,则查询运行正常,但过滤后会出现以下异常:

System.Data.Entity.Core.EntityCommandCompilationException:准备命令定义时出错。有关详细信息,请参阅内部异常。 ---> System.Data.Entity.Core.MetadataException: 导航属性 Fotogoed.Data.Order.Customer 的声明类型与指定导航的结果不兼容。 在 System.Data.Entity.Core.Query.PlanCompiler.PreProcessor.ValidateNavPropertyOp(PropertyOp op)

谁能解释一下这个错误的原因是什么?如果需要,请随时询问更多信息。

【问题讨论】:

    标签: c# asp.net entity-framework ef-code-first entity-framework-6


    【解决方案1】:

    我会在这里为遇到同样问题的人回答我自己的问题。问题是我为 user->customer 添加了一个自定义映射,但忘记将订单列表下推到客户类(这是在用户中)。修复该映射解决了该问题。

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2012-11-26
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多