public void DTO2BD(FrameMaterialColorDTO dtoItem, ref FrameMaterialColor dbItem) { AutoMapper.Mapper.Map <FrameMaterialColorDTO, FrameMaterialColor>(dtoItem, dbItem); }
public bool UpdateDataFrameMaterialColor(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { FrameMaterialColorDTO dtoFrameMaterialColorDTO = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <FrameMaterialColorDTO>(); notification = new Notification(); notification.Type = NotificationType.Success; int number; string indexName; try { // Vailidation ValidationContext validationDTO = new ValidationContext(dtoFrameMaterialColorDTO, null, null); List <ValidationResult> results = new List <ValidationResult>(); bool valid = Validator.TryValidateObject(dtoFrameMaterialColorDTO, validationDTO, results, true); if (!valid) { string error = ""; List <string> detailError = new List <string>(); foreach (ValidationResult vr in results) { notification.Type = NotificationType.Error; error = vr.ErrorMessage; detailError.Add(error); } notification.Type = NotificationType.Error; notification.DetailMessage.AddRange(detailError); return(false); } using (var context = CreateContext()) { FrameMaterialColor frameMaterialColor = null; if (id == 0) { frameMaterialColor = new FrameMaterialColor(); context.FrameMaterialColor.Add(frameMaterialColor); frameMaterialColor.UpdatedBy = userId; frameMaterialColor.UpdatedDate = DateTime.Now; } else { var item = context.FrameMaterialColorMng_FrameMaterialColorCheck_View.Where(o => o.FrameMaterialColorID == id).FirstOrDefault(); //CheckPermission if (item.isUsed.Value == true) { throw new Exception("You can't update because it used in item other!"); } frameMaterialColor = context.FrameMaterialColor.FirstOrDefault(o => o.FrameMaterialColorID == id); frameMaterialColor.UpdatedBy = userId; frameMaterialColor.UpdatedDate = DateTime.Now; } if (frameMaterialColor == null) { notification.Message = "Frame Material Color not found!"; return(false); } else { converter.DTO2BD(dtoFrameMaterialColorDTO, ref frameMaterialColor); if (id <= 0) { // Generate code. using (var trans = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM FrameMaterialColor WITH (TABLOCKX, HOLDLOCK)"); try { var newCode = context.FrameMaterialColorMng_function_GenerateCode().FirstOrDefault(); if (!"**".Equals(newCode)) { frameMaterialColor.FrameMaterialColorUD = newCode; context.SaveChanges(); } else { notification.Type = NotificationType.Error; notification.Message = "Auto generated code exceed maximum option: [ZZ]"; } } catch (Exception ex) { trans.Rollback(); throw ex; } finally { trans.Commit(); } } } else { context.SaveChanges(); } dtoItem = GetData(frameMaterialColor.FrameMaterialColorID, out notification).Data; return(true); } } } catch (DataException exData) { notification.Type = NotificationType.Error; ErrorHelper.DataExceptionParser(exData, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { if ("FrameMaterialColorUDUnique".Equals(indexName)) { notification.Message = "The Frame Material Color Code is already exists."; } } else { notification.Message = exData.Message; } return(false); } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = ex.Message; return(false); } //notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; //try //{ // using (FrameMaterialColorMngEntities context = CreateContext()) // { // FrameMaterialColor dbItem = null; // if (id == 0) // { // dbItem = new FrameMaterialColor(); // context.FrameMaterialColor.Add(dbItem); // } // else // { // dbItem = context.FrameMaterialColor.FirstOrDefault(o => o.FrameMaterialColorID == id); // } // if (dbItem == null) // { // notification.Message = "Frame material color not found!"; // return false; // } // else // { // converter.DTO2BD(dtoItem, ref dbItem); // context.SaveChanges(); // dtoItem = GetData(dbItem.FrameMaterialColorID, out notification).Data; // return true; // } // } //} //catch (Exception ex) //{ // notification.Type = Library.DTO.NotificationType.Error; // notification.Message = ex.Message; // return false; //} }