entity-framework - 级联预加载问题

标签 entity-framework wcf-ria-services eager-loading

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

学生

学生将 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 的导航属性。

如何解决这个问题?

最佳答案

错误是正确的。

IncludeObjectQuery<T> 上您正在查询,在本例中为“学生”。

CountryPerson 的导航属性, 不是 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/

相关文章:

asp.net-mvc - 使用 Entity Framework 从 oracle 数据库创建模型

c# - linq to entities继承查询问题

silverlight - 分页DomainDataSource将所有实体?

c# - 为什么这些异步 RIA 服务调用在 Web 服务器上串行执行?

mysql - Laravel 热切加载关系与添加的子句?

ruby-on-rails - Rails 包含何时添加的方法

c# - 抽象自动映射器预测

entity-framework - Entityframework 使用 join 方法和 lambda 进行连接

c# - 为什么我的 InnerException 没有发送给客户端?

c# - 复杂类型引用的 EagerLoad 实体