예제 #1
0
        public int Update(TEntity entity)
        {
            EntityPersistentSubscribePublisher.OnBeforeUpdate(entity);
            ValidationUnity.Validate(entity);

            var trans = CheckRelationHasModified(entity);

            if (trans)
            {
                context.Database.BeginTransaction();
            }

            int result = 0;

            try
            {
                if ((result = Queryable.UpdateEntity(entity)) > 0)
                {
                    EntityPersistentSubscribePublisher.OnAfterUpdate(entity);
                }

                HandleRelationProperties(entity);

                if (trans)
                {
                    context.Database.CommitTransaction();
                }
            }
            catch (Exception exp)
            {
                if (trans)
                {
                    context.Database.RollbackTransaction();
                }

                throw exp;
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// 更新实体对象的修改。
        /// </summary>
        /// <param name="entity">要更新的实体对象。</param>
        public void Update(IEntity entity)
        {
            ValidationUnity.Validate(entity);
            EntityPersistentSubscribePublisher.OnBeforeUpdate(entity);

            try
            {
                Batch(() =>
                {
                    var parameters = new ParameterCollection();
                    var context    = CreateContext(parameters);
                    var sql        = EntityPersistentQueryBuilder.BuildUpdateQuery(context, entity);

                    var isSucc = false;
                    entity.TryLockModifing(() =>
                    {
                        if (!sql.IsNullOrEmpty())
                        {
                            isSucc = database.ExecuteNonQuery(sql, parameters) > 0;
                        }

                        UpdateRelations(entity);
                    });

                    if (isSucc)
                    {
                        EntityPersistentSubscribePublisher.OnAfterUpdate(entity);
                        entity.SetState(EntityState.Unchanged);
                    }
                });
            }
            catch (Exception exp)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityUpdate), exp);
            }
        }