// ------- // Methods // ------- /// <summary> /// Loads the related entity or entities into the local related end using the supplied MergeOption. /// </summary> public override void Load(MergeOption mergeOption) { CheckOwnerNull(); // Validate that the Load is possible bool hasResults; ObjectQuery <TEntity> sourceQuery = ValidateLoad <TEntity>(mergeOption, "EntityReference", out hasResults); _suppressEvents = true; // we do not want any event during the bulk operation try { List <TEntity> refreshedValue = null; if (hasResults) { // Only issue a query if we know it can produce results (in the case of FK, there may not be any // results). refreshedValue = new List <TEntity>(GetResults <TEntity>(sourceQuery)); } if (null == refreshedValue || refreshedValue.Count == 0) { if (!((AssociationType)base.RelationMetadata).IsForeignKey && ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One) { //query returned zero related end; one related end was expected. throw EntityUtil.LessThanExpectedRelatedEntitiesFound(); } else if (mergeOption == MergeOption.OverwriteChanges || mergeOption == MergeOption.PreserveChanges) { // This entity is not related to anything in this AssociationSet and Role on the server. // If there is an existing _cachedValue, we may need to clear it out, based on the MergeOption EntityKey sourceKey = WrappedOwner.EntityKey; EntityUtil.CheckEntityKeyNull(sourceKey); ObjectStateManager.RemoveRelationships(ObjectContext, mergeOption, (AssociationSet)RelationshipSet, sourceKey, (AssociationEndMember)FromEndProperty); } // else this is NoTracking or AppendOnly, and no entity was retrieved by the Load, so there's nothing extra to do // Since we have no value and are not doing a merge, the last step is to set IsLoaded to true _isLoaded = true; } else if (refreshedValue.Count == 1) { Merge <TEntity>(refreshedValue, mergeOption, true /*setIsLoaded*/); } else { // More than 1 result, which is non-recoverable data inconsistency throw EntityUtil.MoreThanExpectedRelatedEntitiesFound(); } } finally { _suppressEvents = false; } // fire the AssociationChange with Refresh OnAssociationChanged(CollectionChangeAction.Refresh, null); }