级联急切加载问题
假设我有以下从数据库表创建的实体:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是正确的。
Include
位于您正在查询的ObjectQuery
上,在本例中为“Students”。Country
是Person
的导航属性,而不是Student
。将您的代码更改为:
或者简单地说:
因为 EF 将根据嵌套包含自动包含“Person”。
您需要记住,
Include
根据调用它的ObjectQuery
返回一个ObjectQuery
。因此,仅仅因为您执行
Students.Include("Person")
,并不意味着此时变量是ObjectQuery
- 变量仍然是 <代码>ObjectQuery<学生>。The error is correct.
Include
is on theObjectQuery<T>
you are querying, in this case "Students".Country
is a navigational property ofPerson
, notStudent
.Change your code to this:
Or simply:
As EF will automatically include "Person" based on the nested include.
You need to remember that
Include
returns anObjectQuery<T>
based on theObjectQuery<T>
it was invoked upon.So just because your doing
Students.Include("Person")
, that doesn't mean at that point, the variable isObjectQuery<Person>
- the variable is stillObjectQuery<Student>
.