public override int Run(string[] remainingArguments) { var watch = Stopwatch.StartNew(); var outDir = _options.OutputDirectory; try { using (var db = new NopDbContext()) { var pictures = db.Pictures.AsNoTracking(); var imageHashes = SavePicturesToDisk(pictures, outDir); ValidatePictureHashes(imageHashes); if (_options.IsUpdateEnabled) { RemovePicturesFromDb(db); } } Console.Write("ImageExport complete!"); } catch (Exception e) { Console.WriteLine(e.Message); } watch.Stop(); var t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); var timeElapsed = $"{t.Hours:D2}h:{t.Minutes:D2}m:{t.Seconds:D2}s:{t.Milliseconds:D3}ms"; Console.WriteLine(); Console.WriteLine($"Execution time: {timeElapsed}"); // Do something return(0); }
private void RemovePicturesFromDb(NopDbContext db) { var spin = new ConsoleSpinner(); Console.Write("Removing pictures... "); var pictures = db.Pictures; var pictureCount = pictures.Count(); // Clear pictures in DB // Work with only 100 pictures at a time from the DB for (var i = 0; i < pictureCount; i = i + 100) { spin.Turn(); var pictureBatch = pictures.OrderBy(p => p.Id).Skip(i).Take(100); foreach (var picture in pictureBatch) { picture.PictureBinary = new byte[0]; } db.SaveChanges(); } // Turn off StoreInDB setting Console.Write("Turning off StoreInDB setting..."); var setting = db.Settings.SingleOrDefault(s => s.StoreId == _options.StoreId && s.Name == "Media.Images.StoreInDB"); if (setting == null) { return; } setting.Value = false.ToString(); db.SaveChanges(); }