Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Fix for exception when viewing blog posts with query strings
Browse files Browse the repository at this point in the history
The exception message being shown was:

The context can not load the related collection or reference for untracked entities while the 'MergeOption' is not set to 'NoTracking'. Change the 'MergeOption' to 'NoTracking' or attach the 'adx_blogpost' entity.
  • Loading branch information
amervitz committed May 13, 2018
1 parent c36c249 commit 77dc1fa
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Framework/Microsoft.Xrm.Client/EntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,19 @@ private static T GetRelated<T>(

private static T GetRelatedEntity<T>(OrganizationServiceContext context, Entity entity, Relationship relationship) where T : Entity
{
if (!context.IsAttached(entity) && entity.RelatedEntities.Contains(relationship))
if (!context.IsAttached(entity))
{
// clear out the existing related entities
if (entity.RelatedEntities.Contains(relationship))
{
entity.RelatedEntities.Remove(relationship);
}

entity.RelatedEntities.Remove(relationship);
// handle entity already being attached before calling entity.GetRelatedEntity
if (context.GetAttachedEntities().FirstOrDefault(e => e.Id == entity.Id) is Entity attached)
{
entity = attached;
}
}

context.LoadProperty(entity, relationship);
Expand Down

0 comments on commit 77dc1fa

Please sign in to comment.