Пример #1
0
        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();
                }
            }
        }
Пример #2
0
 public static void AttachUpdated(this ObjectContext context, EntityObject objectDetached)
 {
     //if (objectDetached.EntityState == EntityState.Detached)
     if (objectDetached.EntityState == EntityState.Detached || objectDetached.EntityState == EntityState.Modified)//modified by chenyh
     {
         object currentEntityInDb = null;
         if (context.TryGetObjectByKey(objectDetached.EntityKey, out currentEntityInDb))
         {
             context.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached);
             //Apply property changes to all referenced entities in context
             context.ApplyReferencePropertyChanges((IEntityWithRelationships)objectDetached,
                                                   (IEntityWithRelationships)currentEntityInDb);
         }
         else
         {
             throw new ObjectNotFoundException();
         }
     }
 }
Пример #3
0
        public static void AttachUpdated(this ObjectContext context, EntityObject detachedEntity)
        {
            if (detachedEntity.EntityState == EntityState.Detached)
            {
                object currentEntity;

                if (context.TryGetObjectByKey(detachedEntity.EntityKey, out currentEntity))
                {
                    context.ApplyPropertyChanges(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();
                }
            }
        }