public BaseResponse <string> insertNewPais(PaisRequest model) { BaseResponse <string> result = new BaseResponse <string>(); using (MesaDineroContext context = new MesaDineroContext()) { using (var transaccion = context.Database.BeginTransaction()) { try { Tb_MD_Pais vpais = context.Tb_MD_Pais.Find(model.codigo); if (vpais != null) { throw new Exception("Codigo ya existente,Ingrese otro codigo"); } Tb_MD_Pais pais = new Tb_MD_Pais(); pais.IdPais = model.codigo; pais.Nombre = model.nombre; pais.iEstadoRegistro = model.estado; context.Tb_MD_Pais.Add(pais); context.SaveChanges(); transaccion.Commit(); result.success = true; } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { #region Error EntityFramework var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("; ", errorMessages); result.success = false; result.error = fullErrorMessage; transaccion.Rollback(); #endregion } catch (Exception ex) { result.success = false; transaccion.Rollback(); result.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message; } } } return(result); }
public BaseResponse <string> EliminarPais(PaisRequest model) { BaseResponse <string> result = new BaseResponse <string>(); using (MesaDineroContext context = new MesaDineroContext()) { using (var transaccion = context.Database.BeginTransaction()) { try { Tb_MD_Pais pais = context.Tb_MD_Pais.Find(model.codigo); if (pais == null) { throw new Exception("Entidad Nula, Cargo no encontrado"); } pais.iEstadoRegistro = EstadoRegistroTabla.Eliminado; context.SaveChanges(); transaccion.Commit(); result.success = true; } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { #region Error EntityFramework var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); var fullErrorMessage = string.Join("; ", errorMessages); result.success = false; result.error = fullErrorMessage; transaccion.Rollback(); #endregion } catch (Exception ex) { result.success = false; transaccion.Rollback(); result.error = ex.InnerException != null ? ex.InnerException.Message : ex.Message; } } } return(result); }