Question

I have following query in my code:

var query3 = (from b in context.SystemDetails.Include("UserHousing").Include("UserInfo") where (b.UserHousing.UserInfo.FullName.StartsWith("Far")) select b).ToList();

Why does it give error that systemDeail do not have navigation property "UserInfo" .. it should not matter as UserHousing Have that Navigation property...

Était-ce utile?

La solution

You should specify the correct path to UserInfo:

context.SystemDetails.Include("UserHousing.UserInfo")

Or

context.SystemDetails.Include(x => x.UserHousing.UserInfo)

Autres conseils

Your sequence of Include(..) methods is executed against main object being retrieved from context - SystemDetails, and, of course, it doesn't have UserInfo in it.

Paths are all-inclusive. Having both includes replaced with single

Include("UserHousing.UserInfo")

will do the trick. Have a look at documentation for Include(string path) method.

PS. What version of EF are you using? While updating to EF5 I've found following IQueryable extension useful:

DbExtensions.Include<T, TProperty> Method (IQueryable<T>,Expression<Func<T, TProperty>>)

http://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx

See the remarks sections, I believe you are dealing with the following scenario:

  • To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference).

you should end up with something like this in your query:

from b in context.SystemDetails
.Include(detail => detail.UserHousing.UserInfo) ...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top