private static long LoadFolders(DbContainer container, DirectoryInfo origin, int?parentId = null) { var sw = new Stopwatch(); sw.Start(); var originFolder = container.FolderSet.FirstOrDefault(f => f.Name == origin.Name && f.ParentId == parentId); if (originFolder == null) { originFolder = new Folder() { Name = origin.Name, ParentId = parentId }; container.Add(originFolder); container.SaveChanges(); } var dirs = origin.EnumerateDirectories(); foreach (var folder in dirs) { LoadFolders(container, folder, originFolder.Id); } sw.Stop(); return(sw.ElapsedMilliseconds); }
public ActionResult Add(Customer customer) { if (customer != null) { _container.Add(customer); _container.SaveChanges(); TempData["Success"] = "Add New Customer Successed!"; return(RedirectToAction("Index")); } else { TempData["Error"] = "Form Data Required!"; return(View()); } }
public async Task <IActionResult> AddOrEdit([Bind("Id,FullName,Emp_Code,Postion,OfficeLocation")] Employee employee) { if (ModelState.IsValid) { if (employee.Id == 0) { _context.Add(employee); } else { _context.Update(employee); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(employee)); }
private static long LoadFiles(DbContainer container, DirectoryInfo origin, bool update, int?parentId = null) { Console.WriteLine($"Loading Files on folder [{origin.FullName}]."); var sw = new Stopwatch(); sw.Start(); var originFolder = container.FolderSet.FirstOrDefault(f => f.Name == origin.Name && f.ParentId == parentId); if (originFolder != null) { var added = 0; var updated = 0; foreach (var folderFile in origin.EnumerateFiles()) { Console.WriteLine($"[ONGOING] {folderFile.FullName} executionTime: {sw.ElapsedMilliseconds} "); var type = FileTypes.Find(type => type.Name == folderFile.Extension[1..].ToUpper()); if (type == null) { continue; } var file = container.FileSet.FirstOrDefault(f => f.Fullpath == folderFile.FullName); if (file == null) { file = GetFile(folderFile, originFolder.Id, type.Id); container.Add(file); container.SaveChanges(); } var photo = container.PhotoSet.FirstOrDefault(f => f.FileId == file.Id); if (photo == null) { added++; photo = photos.Load(file.Fullpath); if (photo != null) { if (type.Name != FileTypeEnum.CR2.ToString()) { var image = Image.FromFile(file.Fullpath); var thumb = image.Width > 0 && image.Height > 0 ? image.GetThumbnailImage(image.Width / Constants.ImageProperties.ThumbMultiplier, image.Height / Constants.ImageProperties.ThumbMultiplier, () => false, IntPtr.Zero) : image.GetThumbnailImage(Constants.ImageProperties.ThumbWidth, Constants.ImageProperties.ThumbHeight, () => false, IntPtr.Zero); photo.Thumbnail = new ImageConverter().ConvertTo(thumb, typeof(byte[])) as byte[]; image.Dispose(); thumb.Dispose(); } photoRep.Add(photo); container.SaveChanges(); } } else { var newPhoto = photos.Load(file.Fullpath); photo ??= new Photo(); photo.Iso = newPhoto.Iso; photo.Height = newPhoto.Height; photo.Width = newPhoto.Width; photo.FocalLength = newPhoto.FocalLength; photo.FStop = newPhoto.FStop; photo.DateTaken = newPhoto.DateTaken; photoRep.Update(photo); container.SaveChanges(); updated++; } } container.SaveChanges(); Console.WriteLine($"Added: {added}; updated/skipped: {updated}; executionTime: {sw.ElapsedMilliseconds} "); var dirs = origin.EnumerateDirectories(); foreach (var folder in dirs) { LoadFiles(container, folder, true, originFolder.Id); } } sw.Stop(); return(sw.ElapsedMilliseconds); }