A specified Include path is not valid. The EntityType '' does not declare a navigation property with the name ''

为君一笑 提交于 2019-12-04 05:44:55

问题


I wonder if you could help. I get the above error when I call .Include(). It breaks when I include tblMemberships.

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

The reason is because the navigation property between tblCustomerInfo and tblMemberships does not exist. tblUsers is the link between the other two tables.

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

(Sorry, can't include image as my reputataion < 10).

My questions are:

  1. What do I need to do in order to have tblMemberships included?
  2. Is this a recommended way of retrieve data or I should break it up into two queries? Or the design is totally rubbish?

I am using EF5, ASP .NET MVC 4

Kindly advise. Thank you.


回答1:


When you write code like this:

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

You are saying include all entries from ChildTable that are keyed to ParentTable and also include all entries from ChildOfChildTable that are ALSO keyed to ParentTable. Instead you need to tell Entity Framework that ChildOfChildTable is beneath ChildTable in the hierarchy, like this:

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

So this means your code should be:

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


来源:https://stackoverflow.com/questions/25971636/a-specified-include-path-is-not-valid-the-entitytype-does-not-declare-a-navi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!