public PictureDetailModel Insert(PictureDetailModel item) { using (var context = new MyPhotoDbContext()) { var entity = _mapper.MapToEntity(item); entity.Id = Guid.NewGuid(); context.Pictures.Add(entity); context.SaveChanges(); return(_mapper.MapToDetail(entity)); } }
public void Update(AlbumDetailModel item) { using (var context = new MyPhotoDbContext()) { var entity = context.Albums.Include(a => a.PictureCollection).First(album => album.Id == item.Id); entity.Name = item.Name; entity.Description = item.Description; entity.PictureCollection = item.PictureCollection .Select(detailModel => { var pictureEntity = _picMapper.MapToEntity(detailModel); if (context.Pictures.Any(p => p.Id == pictureEntity.Id)) { pictureEntity = context.Pictures.Find(pictureEntity.Id); } return(pictureEntity); } ).ToList(); context.SaveChanges(); } }