public void DTO2DB(DTO.FactoryWarehouse dtoItem, ref FactoryWarehouse dbItem) { if (dtoItem.FactoryWarehousePallets != null) { //check for child rows deleted foreach (FactoryWarehousePallet dbPallet in dbItem.FactoryWarehousePallet.ToArray()) { if (!dtoItem.FactoryWarehousePallets.Select(o => o.FactoryWarehousePalletID).Contains(dbPallet.FactoryWarehousePalletID)) { dbItem.FactoryWarehousePallet.Remove(dbPallet); } } //map child row foreach (DTO.FactoryWarehousePallet dtoPallet in dtoItem.FactoryWarehousePallets) { FactoryWarehousePallet dbPallet; if (dtoPallet.FactoryWarehousePalletID <= 0) { dbPallet = new FactoryWarehousePallet(); dbItem.FactoryWarehousePallet.Add(dbPallet); } else { dbPallet = dbItem.FactoryWarehousePallet.FirstOrDefault(o => o.FactoryWarehousePalletID == dtoPallet.FactoryWarehousePalletID); } if (dbPallet != null) { dtoPallet.CompanyID = dtoItem.CompanyID; AutoMapper.Mapper.Map <DTO.FactoryWarehousePallet, FactoryWarehousePallet>(dtoPallet, dbPallet); } } } AutoMapper.Mapper.Map <DTO.FactoryWarehouse, FactoryWarehouse>(dtoItem, dbItem); dbItem.UpdatedDate = DateTime.Now; }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryWarehouse dtofWarehouse = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryWarehouse>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; List <DTO.FactoryWarehouse> FactoryWarehouseDTOs = new List <DTO.FactoryWarehouse>(); try { using (FactoryWarehouseEntities context = CreateContext()) { FactoryWarehouse dbItem = null; if (id == 0) { dbItem = new FactoryWarehouse(); context.FactoryWarehouse.Add(dbItem); } else { dbItem = context.FactoryWarehouse.FirstOrDefault(o => o.FactoryWarehouseID == id); } if (dbItem == null) { notification.Message = "Warehouse not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtofWarehouse.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } //if (dtofWarehouse.FactoryWarehouseUD != "") //{ // var FactoryWarehouseBDs = context.FactoryWarehouse_FactoryWarehouse_View.Where(o => o.FactoryWarehouseID != id).ToList(); // FactoryWarehouseDTOs = converter.DB2DTO_WarehouseList(FactoryWarehouseBDs); // foreach (DTO.FactoryWarehouse WarehouseData in FactoryWarehouseDTOs) // { // if (dtofWarehouse.FactoryWarehouseUD != null) // { // if (dtofWarehouse.FactoryWarehouseUD == WarehouseData.FactoryWarehouseUD) // { // throw new Exception("This Warehouse Code already exist in the system!"); // } // } // } //} dtofWarehouse.CompanyID = fwFactory.GetCompanyID(userId); converter.DTO2DB(dtofWarehouse, ref dbItem); //remove orphan item context.FactoryWarehousePallet.Local.Where(o => o.FactoryWarehouse == null).ToList().ForEach(o => context.FactoryWarehousePallet.Remove(o)); dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = userId; context.SaveChanges(); dtoItem = GetData(dbItem.FactoryWarehouseID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }