コード例 #1
0
        public void DTO2DB_FactoryMaterialNorm(DTO.FactoryMaterialNorm dtoItem, ref FactoryMaterialNorm dbItem)
        {
            if (dtoItem.FactoryMaterialNormDetails != null)
            {
                foreach (var item in dbItem.FactoryMaterialNormDetail.ToArray())
                {
                    if (!dtoItem.FactoryMaterialNormDetails.Select(s => s.FactoryMaterialNormDetailID).Contains(item.FactoryMaterialNormDetailID))
                    {
                        dbItem.FactoryMaterialNormDetail.Remove(item);
                    }
                }

                foreach (var item in dtoItem.FactoryMaterialNormDetails)
                {
                    //read db image field
                    FactoryMaterialNormDetail dbDetail;
                    if (item.FactoryMaterialNormDetailID < 0)
                    {
                        dbDetail = new FactoryMaterialNormDetail();
                        dbItem.FactoryMaterialNormDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.FactoryMaterialNormDetail.Where(o => o.FactoryMaterialNormDetailID == item.FactoryMaterialNormDetailID).FirstOrDefault();
                    }

                    //map from dto image field to db image field
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactoryMaterialNormDetail, FactoryMaterialNormDetail>(item, dbDetail);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.FactoryMaterialNorm, FactoryMaterialNorm>(dtoItem, dbItem);
        }
コード例 #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            DTO.FactoryMaterialNorm dtoFactoryMaterialNorm = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryMaterialNorm>();
            try
            {
                using (FactoryMaterialNormEntities context = CreateContext())
                {
                    FactoryMaterialNorm dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryMaterialNorm();
                        context.FactoryMaterialNorm.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryMaterialNorm.Where(o => o.FactoryMaterialNormID == id).FirstOrDefault();
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //convert dto to db
                        converter.DTO2DB_FactoryMaterialNorm(dtoFactoryMaterialNorm, ref dbItem);
                        //remove orphan item
                        context.FactoryMaterialNormDetail.Local.Where(o => o.FactoryMaterialNorm == null).ToList().ForEach(o => context.FactoryMaterialNormDetail.Remove(o));
                        //save data
                        context.SaveChanges();
                        //get return data
                        dtoItem = GetData(dbItem.FactoryMaterialNormID, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }