private void SaveChanges() { using (MusicLibraryContext dbContext = new MusicLibraryContext()) { dbContext.Attach(this.data); dbContext.SaveChanges(); } }
private void FavoriteAllSongs(bool favorite) { this.LoadSongs(); using (MusicLibraryContext dbContext = new MusicLibraryContext()) { foreach (Song song in this.data.Songs) { song.Favorite = favorite; dbContext .Attach(song) .Property(_song => _song.Favorite) .IsModified = true; } dbContext.SaveChanges(); } }
private void FavoriteAllSongs(bool favorite) { this.LoadAlbums(); using (MusicLibraryContext dbContext = new MusicLibraryContext()) { foreach (Album album in this.data.Albums) { List <Song> songs = dbContext.Songs.Where(song => song.AlbumId == album.AlbumId).ToList(); foreach (Song song in songs) { song.Favorite = favorite; dbContext .Attach(song) .Property(_song => _song.Favorite) .IsModified = true; } } dbContext.SaveChanges(); } }
protected void SaveChanges() { context.SaveChanges(); }
static void Main(string[] args) { var file = args[0]; Console.WriteLine("Importing records from file: " + file + " ...."); var csv = new CsvHelper.CsvReader(File.OpenText(file)); csv.Configuration.HasHeaderRecord = true; var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); csv.Read(); csv.ReadHeader(); var album = String.Empty; var artists = new Dictionary <string, Artist>(); var ctx = new DbContextOptionsBuilder <MusicLibraryContext>(); ctx.UseSqlServer <MusicLibraryContext>(configuration.GetConnectionString("MusicLibraryDB")); using (var db = new MusicLibraryContext(ctx.Options)) { var alb = new Album(); var art = new Artist(); while (csv.Read()) { var r = csv.GetRecord <TrackRecord>(); if (r.Album != album) { album = r.Album; int y; alb = new Album { Title = r.Album, AlbumArtist = r.AlbumArtist, Year = (Int32.TryParse(r.Year, out y) ? (int?)y : null) }; db.Albums.Add(alb); } if (!artists.ContainsKey(r.Artist)) { art = new Artist { Title = r.Artist, }; db.Artists.Add(art); artists.Add(r.Artist, art); } else { art = artists[r.Artist]; } int dn; var t = new Track { Album = alb, Artist = art, Title = r.Title, TrackNum = r.TrackNum, DiscNum = (Int32.TryParse(r.DiscNum, out dn) ? (int?)dn : null) }; db.Tracks.Add(t); } db.SaveChanges(); } Console.WriteLine("Import complete - press any key to exit..."); Console.ReadLine(); }
public async Task ReadAsync() { this.RaiseProgressUpdate(0, null); int totalFileCount = await FileUtil.GetFilesCountInAllDirectoriesAsync(this.RootFolder); int progressStep = totalFileCount / 1000; int filesAlreadyProcessed = 0; int filesAlreadyProcessedTillLastProgressUpdate = progressStep + 1; // set so that we get status update as soon as we start updating the DB async Task directorySearch(StorageFolder rootFolder) { // recursively search through all folders IReadOnlyList <StorageFolder> folders = await rootFolder.GetFoldersAsync(Windows.Storage.Search.CommonFolderQuery.DefaultQuery); foreach (StorageFolder folder in folders) { await directorySearch(folder); } using (MusicLibraryContext dbContext = new MusicLibraryContext()) { // get all files in directory foreach (StorageFile file in await rootFolder.GetFilesAsync()) { filesAlreadyProcessed++; filesAlreadyProcessedTillLastProgressUpdate++; // continue for sound files only if (allowedExtensions.Contains(file.FileType.ToUpperInvariant())) { Song song; Mp3Stream tagStream = new Id3.Mp3Stream(await file.OpenStreamForReadAsync()); bool anySongTags; Id3Tag[] songTags = null; try { songTags = tagStream.GetAllTags(); anySongTags = songTags.Length > 0; } catch (Exception e) { anySongTags = false; } if (anySongTags) { Id3Tag songTag = songTags[0]; // search for an existing artist by name Artist artist = Artist.CreateOrFind(dbContext, songTag.Artists.Value); // search for an existing album by name and artist Album album = Album.CreateOrFind(dbContext, songTag.Album, artist.ArtistId); // search for an existing song by path song = Song.CreateOrFind(dbContext, file.Path); // set reference to the album song.Album = album; // load other simpler properties if (songTag.Year.IsAssigned) { song.Year = songTag.Year.AsDateTime.Value.Year; } if (songTag.Track.IsAssigned) { song.Track = songTag.Track.AsInt.Value; } song.Title = songTag.Title; song.Genre = songTag.Genre; song.Comment = string.Join(", ", songTag.Comments); // insert/update to DB dbContext.SaveChanges(); // save image to localappdata if (songTag.Pictures.Any()) { await SongImageUtil.SaveImageAsync(song.SongId, songTag.Pictures.First().PictureData, songTag.Pictures.First().MimeType); } } else { song = Song.CreateOrFind(dbContext, file.Path); song.Title = Path.GetFileNameWithoutExtension(file.Path); // insert/update to DB dbContext.SaveChanges(); } // update progress if (filesAlreadyProcessedTillLastProgressUpdate > progressStep) { filesAlreadyProcessedTillLastProgressUpdate = 0; this.RaiseProgressUpdate((float)filesAlreadyProcessed / totalFileCount, song); } } } } }; if (totalFileCount > 0) { await directorySearch(this.RootFolder); } }
public void Save() { _context.SaveChanges(); }