假设我有以下从数据库表创建的实体:
人
学生
学生将 Person 作为导航属性。
Person 具有导航属性 Country 以连接查找表 Country。
在 Student 元数据中,我为导航属性 Person 放置了 [Include]
。
在 Person 元数据中,我将 [Include]
用于导航属性 Country。
加载学生数据时,我想预先加载,例如包含人员和国家/地区数据:
this.ObjectContext.Students.Include("Person").Include("Country");
当我使用以前版本的 ASP.NET Data Ria Service 时,它工作正常。现在,当它更改为 WCF Ria 服务时,上述方式不再有效。 系统报错说 Country 不是 Student 的导航属性。
如何解决这个问题?
最佳答案
错误是正确的。
Include
在 ObjectQuery<T>
上您正在查询,在本例中为“学生”。
Country
是 Person
的导航属性, 不是 Student
.
将您的代码更改为:
this.ObjectContext.Students.Include("Person").Include("Person.Country");
或者简单地说:
this.ObjectContext.Students.Include("Person.Country");
因为 EF 会根据嵌套包含自动包含“Person”。
你需要记住 Include
返回 ObjectQuery<T>
基于 ObjectQuery<T>
它被调用。
所以只是因为你在做 Students.Include("Person")
,这并不意味着此时变量是 ObjectQuery<Person>
- 变量仍然是 ObjectQuery<Student>
.
关于entity-framework - 级联预加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4281672/