public virtual SaveResult CreateRange(IEnumerable <TEntity> entities) { try { var enumerable = entities as TEntity[] ?? entities.ToArray(); foreach (var entity in enumerable) { entity.Estado = EstadoEnum.Activo; entity.FechaCreacion = DateTime.Now; } DbSet.AddRange(enumerable); if (!UnitOfWork.IsInTransaccion) { UnitOfWork.Db.SaveChanges(); } return(SaveResult.Success()); } catch (Exception ex) { return(ex.SaveResult()); } }
public virtual async Task <SaveResult> DeleteAsync(TEntity entity) { try { entity.Estado = EstadoEnum.Eliminado; entity.FechaEliminacion = DateTime.Now; UnitOfWork.Db.Entry(entity).State = EntityState.Modified; if (!UnitOfWork.IsInTransaccion) { await UnitOfWork.SaveChangesAsync(); } return(SaveResult.Success(entity)); } catch (Exception ex) { return(ex.SaveResult()); } }
public virtual async Task <SaveResult> CreateAsync(TEntity entity) { try { entity.Estado = EstadoEnum.Activo; entity.FechaCreacion = DateTime.Now; DbSet.Add(entity); if (!UnitOfWork.IsInTransaccion) { await UnitOfWork.Db.SaveChangesAsync(); } return(SaveResult.Success(entity)); } catch (Exception ex) { return(ex.SaveResult()); } }
public virtual async Task <SaveResult> UpdateAsync(TEntity entity) { try { if (entity.IsNew()) { throw new Exception("No se puede grabar actualizar una entidad nueva"); } entity.FechaModificacion = DateTime.Now; DbSet.Attach(entity); UnitOfWork.Db.Entry(entity).State = EntityState.Modified; if (!UnitOfWork.IsInTransaccion) { await UnitOfWork.SaveChangesAsync(); } return(SaveResult.Success()); } catch (Exception ex) { return(ex.SaveResult()); } }