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(); } } }
/// <summary> /// Helper provisorio para realizar um update no entity que possuir relacionamentos. /// </summary> /// <param name="context">Contexto do Entity.</param> /// <param name="entitySetName">Nome do EntitySet.</param> /// <param name="entity">Entity Object.</param> public static void UpdateWithReference(this ObjectContext context, string entitySetName, EntityObject entity) { if (entity.EntityKey == null) entity.EntityKey = context.CreateEntityKey(context.DefaultContainerName + "." + entitySetName, entity); if (entity.EntityState == EntityState.Detached) { object currentEntityInDb; if (context.TryGetObjectByKey(entity.EntityKey, out currentEntityInDb)) { context.ApplyPropertyChanges(entity.EntityKey.EntitySetName, entity); context.ApplyReferencePropertyChanges(entity, (IEntityWithRelationships)currentEntityInDb); //extension } else { throw new ObjectNotFoundException(); } } }