开发者

Cascade eager-loading problem

开发者 https://www.devze.com 2023-01-27 04:57 出处:网络
Supose I have following entities created from database tables: Person Student Student include Person as navigation property.

Supose I have following entities created from database tables:

Person

Student

Student include Person as navigation property.

Person has navigation property Country to connect lookup table Country.

In Student metadata, I do put [Include] for navigation property Person. In Person metadata, I do put [Include] for navigation property Country.

When loading student data, I want to eager loading like to include Person and Country data:

this.ObjectContext.Students.Include("Person").Include("Country");

This was working fine when I use previous version of ASP.NET Data Ria Service. Now when it is changed to WCF Ria Service, above way not working any more. System give me the开发者_开发知识库 error said Country is not a navigation property of Student.

How to resolve this problem?


The error is correct.

Include is on the ObjectQuery<T> you are querying, in this case "Students".

Country is a navigational property of Person, not Student.

Change your code to this:

this.ObjectContext.Students.Include("Person").Include("Person.Country");

Or simply:

this.ObjectContext.Students.Include("Person.Country");

As EF will automatically include "Person" based on the nested include.

You need to remember that Include returns an ObjectQuery<T> based on the ObjectQuery<T> it was invoked upon.

So just because your doing Students.Include("Person"), that doesn't mean at that point, the variable is ObjectQuery<Person> - the variable is still ObjectQuery<Student>.

0

精彩评论

暂无评论...
验证码 换一张
取 消