static void PruneDbsFromFilesystem() { var Context = new Models.TRContext(); foreach (var item in new DirectoryInfo("dbs").GetFiles("*.db")) { try { if ((DateTime.Now - item.LastAccessTime).TotalDays > DBPrunePeriodInDays) { if (Guid.TryParse(Path.GetFileNameWithoutExtension(item.FullName), out Guid publicKey)) { var app = Context.Applications.Where(q => q.PublicKey == publicKey).FirstOrDefault(); Context.Applications.Remove(app); Context.SaveChanges(); } item.Delete(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }