internal void AttachIdentity(EntityDescriptor entityDescriptorFromMaterializer, MergeOption metadataMergeOption) { this.EnsureIdentityToResource(); EntityDescriptor descriptor = this.entityDescriptors[entityDescriptorFromMaterializer.Entity]; this.ValidateDuplicateIdentity(entityDescriptorFromMaterializer.Identity, descriptor); this.DetachResourceIdentity(descriptor); if (descriptor.IsDeepInsert) { LinkDescriptor descriptor2 = this.bindings[descriptor.GetRelatedEnd(this.maxProtocolVersion)]; descriptor2.State = EntityStates.Unchanged; } descriptor.Identity = entityDescriptorFromMaterializer.Identity; AtomMaterializerLog.MergeEntityDescriptorInfo(descriptor, entityDescriptorFromMaterializer, true, metadataMergeOption); descriptor.State = EntityStates.Unchanged; this.identityToDescriptor[entityDescriptorFromMaterializer.Identity] = descriptor; }
/// <summary>Applies all accumulated changes to the associated data context.</summary> /// <remarks>The log should be cleared after this method successfully executed.</remarks> internal void ApplyToContext() { if (!this.Tracking) { return; } foreach (KeyValuePair <String, ODataEntry> entity in this.identityStack) { // Try to attach the entity descriptor got from materializer, if one already exists, get the existing reference instead. MaterializerEntry entry = MaterializerEntry.GetEntry(entity.Value); bool mergeEntityDescriptorInfo = entry.CreatedByMaterializer || entry.ResolvedObject == this.insertRefreshObject || entry.ShouldUpdateFromPayload; // Whenever we merge the data, only at those times will be merge the links also EntityDescriptor descriptor = this.entityTracker.InternalAttachEntityDescriptor(entry.EntityDescriptor, false /*failIfDuplicated*/); AtomMaterializerLog.MergeEntityDescriptorInfo(descriptor, entry.EntityDescriptor, mergeEntityDescriptorInfo, this.mergeOption); if (mergeEntityDescriptorInfo) { // In AtomMaterializer.TryResolveFromContext, we set AtomEntry.ShouldUpdateFromPayload to true // when even MergeOption is PreserveChanges and entityState is Deleted. But in that case, we cannot // set the entity state to Unchanged, hence need to workaround that one scenario if (this.mergeOption != MergeOption.PreserveChanges || descriptor.State != EntityStates.Deleted) { // we should always reset descriptor's state to Unchanged (old v1 behaviour) descriptor.State = EntityStates.Unchanged; } } } foreach (LinkDescriptor link in this.links) { if (EntityStates.Added == link.State) { // Added implies collection this.entityTracker.AttachLink(link.Source, link.SourceProperty, link.Target, this.mergeOption); } else if (EntityStates.Modified == link.State) { // Modified implies reference object target = link.Target; if (MergeOption.PreserveChanges == this.mergeOption) { // GetLinks looks up the existing link using just the SourceProperty, the declaring server type name is not significant here. LinkDescriptor end = this.entityTracker.GetLinks(link.Source, link.SourceProperty).SingleOrDefault(); if (null != end && null == end.Target) { // leave the SetLink(link.Source, link.SourceProperty, null) continue; } if ((null != target) && (EntityStates.Deleted == this.entityTracker.GetEntityDescriptor(target).State) || (EntityStates.Deleted == this.entityTracker.GetEntityDescriptor(link.Source).State)) { target = null; } } this.entityTracker.AttachLink(link.Source, link.SourceProperty, target, this.mergeOption); } else { // detach link Debug.Assert(EntityStates.Detached == link.State, "not detached link"); this.entityTracker.DetachExistingLink(link, false); } } }