Why does LoadProperty not load the related entities for 1:N relationships? (CRM2011, early binding entity classes)

c#, dynamics-crm-2011

Solution

Just because you think it should return an empty list when the object doesn't have any child entities doesn't mean that's the way LoadProperty works.

So for anyone else who comes upon this:

LoadProperty will leave the property null when there aren't any related records for that record, even on 1:N relationships.

Problem

Suppose I have a custom entity `new_someentity` which has 2 other related entities: an "owner" entity which I'll call `new_ownerentity` (this is a N:1 relationship) and a "child" entity which I'll call `new_childentity` (1:N relationship). I'm attempting to populate the related entities by calling `LoadProperty`: ``` new_someentity en = context.new_someentitySet.First(); context.LoadProperty(en, "new_someentity_new_ownerentity"); context.LoadProperty(en, "new_someentity_new_childentity"); ``` Afterward, `en.new_someentity_new_ownerentity` is populated as I expect it to be with a reference to the owner entity, but `en.new_someentity_new_childentity` is simply still null. No errors are produced. What's the deal? On a side note, is there really not a concise way to load a related entity for an IEnumerable of entities without needing to use `LoadProperty` on each entity individually? This seems like a pretty classic case of an N+1 queries issue.

Original source