private void UpdateEntry <TEntity>(IEntityWrapper wrappedEntity, EntityEntry existingEntry) { Type type = typeof(TEntity); if (type != existingEntry.WrappedEntity.IdentityType) { EntityKey entityKey = existingEntry.EntityKey; throw new NotSupportedException(Strings.Materializer_RecyclingEntity((object)TypeHelpers.GetFullName(entityKey.EntityContainerName, entityKey.EntitySetName), (object)type.FullName, (object)existingEntry.WrappedEntity.IdentityType.FullName, (object)entityKey.ConcatKeyValue())); } if (EntityState.Added == existingEntry.State) { throw new InvalidOperationException(Strings.Materializer_AddedEntityAlreadyExists((object)existingEntry.EntityKey.ConcatKeyValue())); } if (this.MergeOption == MergeOption.AppendOnly) { return; } if (MergeOption.OverwriteChanges == this.MergeOption) { if (EntityState.Deleted == existingEntry.State) { existingEntry.RevertDelete(); } existingEntry.UpdateCurrentValueRecord(wrappedEntity.Entity); this.Context.ObjectStateManager.ForgetEntryWithConceptualNull(existingEntry, true); existingEntry.AcceptChanges(); this.Context.ObjectStateManager.FixupReferencesByForeignKeys(existingEntry, true); } else if (EntityState.Unchanged == existingEntry.State) { existingEntry.UpdateCurrentValueRecord(wrappedEntity.Entity); this.Context.ObjectStateManager.ForgetEntryWithConceptualNull(existingEntry, true); existingEntry.AcceptChanges(); this.Context.ObjectStateManager.FixupReferencesByForeignKeys(existingEntry, true); } else if (this.Context.ContextOptions.UseLegacyPreserveChangesBehavior) { existingEntry.UpdateRecordWithoutSetModified(wrappedEntity.Entity, (DbUpdatableDataRecord)existingEntry.EditableOriginalValues); } else { existingEntry.UpdateRecordWithSetModified(wrappedEntity.Entity, (DbUpdatableDataRecord)existingEntry.EditableOriginalValues); } }
private void UpdateEntry <TEntity>(IEntityWrapper wrappedEntity, EntityEntry existingEntry) { DebugCheck.NotNull(wrappedEntity); DebugCheck.NotNull(wrappedEntity.Entity); DebugCheck.NotNull(existingEntry); DebugCheck.NotNull(existingEntry.Entity); var clrType = typeof(TEntity); if (clrType != existingEntry.WrappedEntity.IdentityType) { var key = existingEntry.EntityKey; throw new NotSupportedException( Strings.Materializer_RecyclingEntity( TypeHelpers.GetFullName(key.EntityContainerName, key.EntitySetName), clrType.FullName, existingEntry.WrappedEntity.IdentityType.FullName)); } if (EntityState.Added == existingEntry.State) { throw new InvalidOperationException(Strings.Materializer_AddedEntityAlreadyExists(clrType.FullName)); } if (MergeOption.AppendOnly != MergeOption) { // existing entity, update CSpace values in place Debug.Assert(EntityState.Added != existingEntry.State, "entry in State=Added"); Debug.Assert(EntityState.Detached != existingEntry.State, "entry in State=Detached"); if (MergeOption.OverwriteChanges == MergeOption) { if (EntityState.Deleted == existingEntry.State) { existingEntry.RevertDelete(); } existingEntry.UpdateCurrentValueRecord(wrappedEntity.Entity); Context.ObjectStateManager.ForgetEntryWithConceptualNull(existingEntry, resetAllKeys: true); existingEntry.AcceptChanges(); Context.ObjectStateManager.FixupReferencesByForeignKeys(existingEntry, replaceAddedRefs: true); } else { Debug.Assert(MergeOption.PreserveChanges == MergeOption, "not MergeOption.PreserveChanges"); if (EntityState.Unchanged == existingEntry.State) { // same behavior as MergeOption.OverwriteChanges existingEntry.UpdateCurrentValueRecord(wrappedEntity.Entity); Context.ObjectStateManager.ForgetEntryWithConceptualNull(existingEntry, resetAllKeys: true); existingEntry.AcceptChanges(); Context.ObjectStateManager.FixupReferencesByForeignKeys(existingEntry, replaceAddedRefs: true); } else { if (Context.ContextOptions.UseLegacyPreserveChangesBehavior) { // Do not mark properties as modified if they differ from the entity. existingEntry.UpdateRecordWithoutSetModified(wrappedEntity.Entity, existingEntry.EditableOriginalValues); } else { // Mark properties as modified if they differ from the entity existingEntry.UpdateRecordWithSetModified(wrappedEntity.Entity, existingEntry.EditableOriginalValues); } } } } }