【问题标题】:A specified Include path is not valid. The EntityType '' does not declare a navigation property with the name ''指定的包含路径无效。 EntityType '' 未声明名为 '' 的导航属性
【发布时间】:2014-09-22 19:50:51
【问题描述】:

不知道你能不能帮忙。当我调用.Include() 时,我收到了上述错误。当我包含 tblMemberships 时它会中断。

this.dbContext.Configuration.LazyLoadingEnabled = false;
List<tblCustomerInfo> customers = this.dbContext.tblCustomerInfoes.Include("tblUsers").Include("tblMemberships").ToList();

原因是tblCustomerInfotblMemberships 之间的导航属性不存在。 tblUsers 是其他两个表之间的链接。

Customer -- 1:* -- User -- 1:* -- Membership

(对不起,不能将图片作为我的声誉

我的问题是:

  1. 我需要做什么才能包含tblMemberships
  2. 这是一种推荐的数据检索方式,还是我应该将其分成两个查询?还是设计完全是垃圾?

我正在使用 EF5、ASP .NET MVC 4

请多多指教。谢谢。

【问题讨论】:

  • 也许.Include("tblUsers.tblMemberships") 是正确的方法?
  • @DavidG Ahh..... 太好了!谢谢!

标签: asp.net-mvc-4 entity-framework-5


【解决方案1】:

当你编写这样的代码时:

db.ParentTable
    .Include("ChildTable")
    .Include("ChildOfChildTable");

您的意思是包括来自ChildTable 的所有与ParentTable 键控的条目,还包括来自ChildOfChildTable 的所有也与ParentTable 键控的条目。相反,您需要告诉 Entity Framework ChildOfChildTable 在层次结构中位于 ChildTable 之下,如下所示:

db.ParentTable
    .Include("ChildTable.ChildOfChildTable");

所以这意味着你的代码应该是:

this.dbContext.Configuration.LazyLoadingEnabled = false;
List<tblCustomerInfo> customers = this.dbContext.tblCustomerInfoes
                                      .Include("tblUsers.tblMemberships")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多