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); }
public static EntityKey GetEntityKey(this IRelatedEnd relatedEnd) { Debug.Assert(relatedEnd.IsEntityReference()); Type relationshipType = relatedEnd.GetType(); PropertyInfo pi = relationshipType.GetProperty("EntityKey"); return((EntityKey)pi.GetValue(relatedEnd, null)); }
public static void SetEntityKey(this IRelatedEnd relatedEnd, EntityKey key) { Debug.Assert(relatedEnd.IsEntityReference()); Type relationshipType = relatedEnd.GetType(); PropertyInfo pi = relationshipType.GetProperty("EntityKey"); pi.SetValue(relatedEnd, key, null); }
public static bool TryGetObjectByKey(this ObjectContext context, ObjectStateEntry entry, IRelatedEnd relatedEnd, out object relatedEndEntity) { Debug.Assert(relatedEnd.IsEntityReference()); AssociationSetEnd associationSetEnd = (from ase in ((AssociationSet)relatedEnd.RelationshipSet).AssociationSetEnds where ase.Name.Equals(relatedEnd.TargetRoleName) select ase).First(); MetadataProperty metaDataProperty = (from mp in associationSetEnd.MetadataProperties where mp.Name.Equals("EntitySet") select mp).First(); EntitySet entitySet = (EntitySet)metaDataProperty.GetType().GetProperty("Value").GetValue(metaDataProperty); IEnumerable <EntityKeyMember> keyMembers = from km in entitySet.ElementType.KeyMembers select new EntityKeyMember(km.Name, entry.OriginalValues[km.Name]); return(context.TryGetObjectByKey(new EntityKey(entitySet.EntityContainer.Name + "." + entitySet.Name, keyMembers), out relatedEndEntity)); }