public static object CreateOriginalValuesObjectWithReferences(this ObjectContext context, object source) { object target = context.CreateOriginalValuesObject(source); EntityKey srcKey = ((IEntityWithKey)source).EntityKey; IEntityWithRelationships sourceWithRelationships = source as IEntityWithRelationships; if (sourceWithRelationships == null) { return(target); } foreach (var relationshipGroup in context.GetRelationshipsByRelatedEnd((IEntityWithKey)target, EntityState.Unchanged | EntityState.Deleted)) { IRelatedEnd tgtRelatedEnd = (IRelatedEnd)relationshipGroup.Key; foreach (ObjectStateEntry srcEntry in relationshipGroup) { if (tgtRelatedEnd.IsEntityReference()) { tgtRelatedEnd.SetEntityKey(srcEntry.OtherEndKey(srcKey)); } } } return(target); }