public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FrameMaterialProfile dtoFrameMaterialProfile = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FrameMaterialProfile>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FrameMaterialProfileMngEntities context = CreateContext()) { FrameMaterialProfile dbItem = null; if (id == 0) { dbItem = new FrameMaterialProfile(); context.FrameMaterialProfile.Add(dbItem); } else { dbItem = context.FrameMaterialProfile.FirstOrDefault(o => o.FrameMaterialProfileID == id); } if (dbItem == null) { notification.Message = "Profile not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFrameMaterialProfile.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } converter.DTO2BD(dtoFrameMaterialProfile, ref dbItem); context.SaveChanges(); // processing image if (dtoFrameMaterialProfile.ImageFile_HasChange) { dbItem.ImageFile = fwFactory.CreateFilePointer(this._tempFolder, dtoFrameMaterialProfile.ImageFile_NewFile, dtoFrameMaterialProfile.ImageFile); } context.SaveChanges(); dtoItem = GetData(dbItem.FrameMaterialProfileID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public void DTO2BD(DTO.FrameMaterialProfile dtoItem, ref FrameMaterialProfile dbItem) { // map factory if (dtoItem.FrameMaterialProfileFactories != null) { // check for child rows deleted foreach (FrameMaterialProfileFactory dbFactory in dbItem.FrameMaterialProfileFactory.ToArray()) { if (!dtoItem.FrameMaterialProfileFactories.Select(o => o.FrameMaterialProfileFactoryID).Contains(dbFactory.FrameMaterialProfileFactoryID)) { dbItem.FrameMaterialProfileFactory.Remove(dbFactory); } } // map child rows foreach (DTO.FrameMaterialProfileFactory dtoFactory in dtoItem.FrameMaterialProfileFactories) { FrameMaterialProfileFactory dbFactory; if (dtoFactory.FrameMaterialProfileFactoryID <= 0) { dbFactory = new FrameMaterialProfileFactory(); dbItem.FrameMaterialProfileFactory.Add(dbFactory); } else { dbFactory = dbItem.FrameMaterialProfileFactory.FirstOrDefault(o => o.FrameMaterialProfileFactoryID == dtoFactory.FrameMaterialProfileFactoryID); } if (dbFactory != null) { AutoMapper.Mapper.Map <DTO.FrameMaterialProfileFactory, FrameMaterialProfileFactory>(dtoFactory, dbFactory); } } } AutoMapper.Mapper.Map <DTO.FrameMaterialProfile, FrameMaterialProfile>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; }