【问题标题】:Why does LoadProperty not load the related entities for 1:N relationships? (CRM2011, early binding entity classes)为什么 LoadProperty 不为 1:N 关系加载相关实体? (CRM2011,早期绑定实体类)
【发布时间】:2013-07-18 22:42:00
【问题描述】:

假设我有一个自定义实体 new_someentity,它有 2 个其他相关实体:一个“所有者”实体,我将其称为 new_ownerentity(这是 N:1 关系)和一个“子”实体,我会打电话给new_childentity(1:N 关系)。

我正在尝试通过调用LoadProperty 来填充相关实体:

new_someentity en = context.new_someentitySet.First();
context.LoadProperty(en, "new_someentity_new_ownerentity");
context.LoadProperty(en, "new_someentity_new_childentity");

之后,en.new_someentity_new_ownerentity 被填充,正如我期望的那样,它会引用所有者实体,但 en.new_someentity_new_childentity 仍然是空的。不会产生错误。

怎么了?

附带说明一下,真的没有一种简洁的方法可以为 IEnumerable 实体加载相关实体,而无需在每个实体上单独使用 LoadProperty?这似乎是 N+1 查询问题的一个非常经典的案例。

【问题讨论】:

  • 远射,但你确定你的 new_someentity 记录有一些 new_childentity 记录吗?
  • @AnthonySteven 你真的做到了。在试图提炼出一个不同的问题时,我懒惰地使用 First() 来获取一个实体,但我没有意识到那个特定的记录没有任何孩子。我真正的问题是我假设如果它没有孩子 LoadProperty 将创建一个空的 IEnumerable 而不是让它为空

标签: c# dynamics-crm-2011


【解决方案1】:

仅仅因为您认为当对象没有任何子实体时它应该返回一个空列表并不意味着这就是 LoadProperty 的工作方式。

所以对于遇到此问题的其他人:

当该记录没有任何相关记录时,即使是 1:N 关系,LoadProperty 也会将该属性保留为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多