Пример #1
0
        public static SONG AddSongToDB(string name, string artistName, string albumName, List <string> genreNames, int year, int durationSec)
        {
            var context = DJPlaylistContext.GetContext();

            var song = context.SONGS.FirstOrDefault(i => i.NAME == name && i.ARTIST_NAME == artistName && i.ALBUM_NAME == albumName && i.YEAR == year);

            if (song == null)
            {
                song = new SONG(name, artistName, albumName, year, durationSec);
                context.SONGS.Add(song);
                context.SaveChanges();
            }

            foreach (var genreName in genreNames)
            {
                var genre = context.GENRES.FirstOrDefault(i => i.DESCRIPTION == genreName);
                if (genre == null)
                {
                    genre = new GENRE(genreName);
                    context.GENRES.Add(genre);
                    context.SaveChanges();
                }
                var songGenre = context.SONG_GENRES.FirstOrDefault(i => i.SONG_ID == song.ID && i.GENRE_ID == genre.ID);
                if (songGenre == null)
                {
                    songGenre = new SONG_GENRE(song, genre);
                    context.SONG_GENRES.Add(songGenre);
                    context.SaveChanges();
                }
            }

            return(song);
        }
Пример #2
0
 public SONG_GENRE(SONG song, GENRE genre)
 {
     SONG_ID  = song.ID;
     GENRE_ID = genre.ID;
 }