public static void AttachUpdated(this ObjectContext objContext, EntityObject objDetached) { if (objDetached.EntityState == EntityState.Detached) { object original = null; if (objContext.TryGetObjectByKey(objDetached.EntityKey, out original)) { objContext.ApplyCurrentValues(objDetached.EntityKey.EntitySetName, objDetached); } else { throw new ObjectNotFoundException(); } } }
public static void AttachUpdated(this ObjectContext context, EntityObject detachedEntity) { #region EntityKey is null if (detachedEntity.EntityKey == null) { //String entitySetName = GetEntitySetFullName(context, detachedEntity); //int objectId = (int)detachedEntity.GetType().GetProperty("Id").GetValue(detachedEntity, null); ////detachedEntity.EntityKey = new System.Data.EntityKey(entitySetName, "Id", objectId); //detachedEntity.EntityKey = new EntityKey(context.GetEntitySetName(detachedEntity.GetType())); //// create a new primary key //Guid newPk = Guid.NewGuid(); //// create new entity key //EntityKey entityKey = new EntityKey(context.GetType().Name + "." + detachedEntity.ToString(), pkName, newPk); //// get type name of new entity //String typeName = oc.GetType().Namespace + "." + entityKey.EntitySetName; //// create a new entity } #endregion if (detachedEntity.EntityState == EntityState.Detached) { object currentEntity = null; if (context.TryGetObjectByKey(detachedEntity.EntityKey, out currentEntity)) { context.ApplyCurrentValues(detachedEntity.EntityKey.EntitySetName, detachedEntity); var newEntity = detachedEntity as IEntityWithRelationships; var oldEntity = currentEntity as IEntityWithRelationships; if (newEntity != null && oldEntity != null) { context.ApplyReferencePropertyChanges(newEntity, oldEntity); } } else { throw new ObjectNotFoundException(); } } }
public static void AttachUpdated(this ObjectContext context, EntityObject objectDetached) { if (objectDetached.EntityState == EntityState.Detached) { object currentEntityInDb = null; if (context.TryGetObjectByKey(objectDetached.EntityKey, out currentEntityInDb)) { context.ApplyCurrentValues(objectDetached.EntityKey.EntitySetName, objectDetached); //context.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached); //(CDLTLL)Apply property changes to all referenced entities in context context.ApplyReferencePropertyChanges((IEntityWithRelationships)objectDetached, (IEntityWithRelationships)currentEntityInDb); //Custom extensor method } else { throw new ObjectNotFoundException(); } } }