public bool UpdateSupplierPriceList(SupplierPriceList updateSupplierPriceList, bool newVer = false) { try { if (updateSupplierPriceList == null) { throw new ArgumentNullException("updateSupplierPriceList"); } using (var db = new HKSupplyContext()) { using (var dbTrans = db.Database.BeginTransaction()) { try { updateSupplierPriceList.IdSubVer += 1; if (newVer == true) { updateSupplierPriceList.IdVer += 1; updateSupplierPriceList.IdSubVer = 0; } updateSupplierPriceList.Timestamp = DateTime.Now; SupplierPriceListHistory supplierPriceListHistory = (SupplierPriceListHistory)updateSupplierPriceList; supplierPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin; //Con esto marcaremos todo el objeto como modificado y actualizará todos los campos. //En este caso nos interesa porque la mayoría de los campos de supplier se pueden modificar db.Entry(updateSupplierPriceList).State = EntityState.Modified; db.SuppliersPriceListHistory.Add(supplierPriceListHistory); db.SaveChanges(); dbTrans.Commit(); return(true); } catch (SqlException sqlex) { dbTrans.Rollback(); for (int i = 0; i < sqlex.Errors.Count; i++) { _log.Error("Index #" + i + "\n" + "Message: " + sqlex.Errors[i].Message + "\n" + "Error Number: " + sqlex.Errors[i].Number + "\n" + "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" + "Source: " + sqlex.Errors[i].Source + "\n" + "Procedure: " + sqlex.Errors[i].Procedure + "\n"); switch (sqlex.Errors[i].Number) { case -1: //connection broken case -2: //timeout throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError")); } } throw sqlex; } catch (Exception ex) { dbTrans.Rollback(); _log.Error(ex.Message, ex); throw ex; } } } } catch (ArgumentNullException anex) { _log.Error(anex.Message, anex); throw anex; } }
public bool UpdateCustomersPricesList(IEnumerable <CustomerPriceList> pricesListToUpdate) { try { if (pricesListToUpdate == null) { throw new ArgumentNullException(nameof(pricesListToUpdate)); } using (var db = new HKSupplyContext()) { using (var dbTrans = db.Database.BeginTransaction()) { try { foreach (var priceList in pricesListToUpdate) { priceList.IdSubVer += 1; priceList.Timestamp = DateTime.Now; CustomerPriceListHistory customerPriceListHistory = (CustomerPriceListHistory)priceList; customerPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin; //Con esto marcaremos todo el objeto como modificado y actualizará todos los campos. //En este caso nos interesa porque la mayoría de los campos de supplier se pueden modificar db.Entry(priceList).State = EntityState.Modified; db.CustomersPriceListHistory.Add(customerPriceListHistory); } db.SaveChanges(); dbTrans.Commit(); return(true); } catch (SqlException sqlex) { dbTrans.Rollback(); for (int i = 0; i < sqlex.Errors.Count; i++) { _log.Error("Index #" + i + "\n" + "Message: " + sqlex.Errors[i].Message + "\n" + "Error Number: " + sqlex.Errors[i].Number + "\n" + "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" + "Source: " + sqlex.Errors[i].Source + "\n" + "Procedure: " + sqlex.Errors[i].Procedure + "\n"); switch (sqlex.Errors[i].Number) { case -1: //connection broken case -2: //timeout throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError")); } } throw sqlex; } catch (DbEntityValidationException e) { dbTrans.Rollback(); _log.Error(e.Message, e); throw e; } catch (Exception ex) { dbTrans.Rollback(); _log.Error(ex.Message, ex); throw ex; } } } } catch (ArgumentNullException nrex) { _log.Error(nrex.Message, nrex); throw nrex; } }