/// <summary> /// 使用一个参照的实体对象更新满足条件的一序列对象。 /// </summary> /// <param name="entity">更新的参考对象。</param> /// <param name="predicate">用于测试每个元素是否满足条件的函数。</param> /// <returns>影响的实体数。</returns> public int Update(TEntity entity, Expression <Func <TEntity, bool> > predicate) { int result; if ((result = repositoryProxy.Update(entity, predicate)) > 0) { EntityPersistentSubscribePublisher.OnAfterUpdate(entity); } return(result); }
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); }
/// <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); } }