级联急切加载问题

发布于 2024-10-04 21:10:09 字数 529 浏览 1 评论 0原文

假设我有以下从数据库表创建的实体:

Person

Student

Student 包括 Person 作为导航属性。

Person 具有导航属性 Country 来连接查找表 Country。

在学生元数据中,我确实将 [Ininclude] 作为导航属性 Person。 在人员元数据中,我确实将 [Ininclude] 作为导航属性国家/地区。

加载学生数据时,我想急切地加载包括人员和国家/地区数据:

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

当我使用以前版本的 ASP.NET Data Ria 服务时,这工作正常。现在,当它更改为 WCF Ria 服务时,上述方法不再起作用。 系统给我错误说国家不是学生的导航属性。

如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2024-10-11 21:10:50

错误是正确的。

Include 位于您正在查询的 ObjectQuery 上,在本例中为“Students”。

CountryPerson 的导航属性,而不是 Student

将您的代码更改为:

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

或者简单地说:

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

因为 EF 将根据嵌套包含自动包含“Person”。

您需要记住,Include 根据调用它的 ObjectQuery 返回一个 ObjectQuery

因此,仅仅因为您执行 Students.Include("Person"),并不意味着此时变量是 ObjectQuery - 变量仍然是 <代码>ObjectQuery<学生>。

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>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文