예제 #1
0
        public void Update(Music model)
        {
            if (model == null || model.Artist == null || model.Genre == null)
            {
                throw new ElementUpdateException <Music>(model, "Element incomplete.");
            }

            EntityModel.Music  oldMusic = _context.Musics.Find(model.ID);
            EntityModel.Artist artist   = _mapper.Map <Artist, EntityModel.Artist>(model.Artist);
            EntityModel.Genre  genre    = _mapper.Map <Genre, EntityModel.Genre>(model.Genre);

            if (oldMusic == null)
            {
                throw new ElementUpdateException <Music>(model, "Element to update doesn't exist.");
            }

            oldMusic.Name = model.Name;
            //oldMusic.Artist = artist;
            oldMusic.ArtistID = (int)artist.ID;
            //oldMusic.Genre = genre;
            oldMusic.GenreID = (int)genre.ID;

            try
            {
                _context.Musics.Update(oldMusic);
                _context.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                throw new ElementUpdateException <Music>(model, "Element to update doesn't exist.", ex);
            }
        }
예제 #2
0
        public void DeleteById(int id)
        {
            EntityModel.Music music = _context.Musics.Find(id);

            if (music == null)
            {
                throw new ElementDeleteException <Music>(id, "Element to delete doesn't exist.");
            }

            try
            {
                _context.Musics.Remove(music);
                _context.SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                throw new ElementDeleteException <Music>(id, "Unable to delete.", ex);
            }
        }