void Save(String fullAddress, String uik) { using (var ctx = new Context()) { var sepIndex = fullAddress.LastIndexOf(", "); String parentAddress = fullAddress.Substring(0, sepIndex).Trim(); String address = fullAddress.Substring(sepIndex+1).Trim(); //var addrList = fullAddress.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).ToList(); var parentNode = ctx.Nodes.Where(n => n.Address == parentAddress).FirstOrDefault(); if (parentNode == null) parentNode = new Node() { Address = parentAddress }; var currentNode = new Node() { Address = address, Uik = uik, Parent = parentNode }; ctx.Nodes.Add(currentNode); try { ctx.SaveChanges(); } catch (Exception ex) { throw ex; } } }
static void Main(string[] args) { System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionWrapper; try { Logger.Instance.Info("Старт приложения"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); var miner = new DbCreator(); var filePath = GetProcessedFilePath(); var resultPath = Path.GetDirectoryName(filePath) + "\\" + Path.GetFileNameWithoutExtension(filePath) + "_result" + Path.GetExtension(filePath); Logger.Instance.Info("Преобразуем файл " + filePath); // очищаем базу using(var ctx = new Context()) { ctx.Database.ExecuteSqlCommand("DELETE FROM Nodes"); } // создаем базу данных miner.FillDatabase(filePath); BlockingCollection<AddressInfo> queue = new BlockingCollection<AddressInfo>(); CalcFlatWorker calcFlatWorker = new CalcFlatWorker(); FileWriteWorker fileWriteWorker = new FileWriteWorker(); CancellationTokenSource cancelSource = new CancellationTokenSource(); var tasks = new List<Task>(); tasks.Add(Task.Run(() => calcFlatWorker.Calc(queue, cancelSource.Token))); tasks.Add(Task.Run(() => fileWriteWorker.Write(queue, resultPath, cancelSource.Token))); while (true) { Task.Delay(1000).Wait(); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); if (key.Key == ConsoleKey.Q) { Logger.Instance.Info("Прервано по команде пользователя!"); break; } } if (queue.IsCompleted) break; } Task.WaitAll(tasks.ToArray()); Logger.Instance.Info("Результирующий файл " + resultPath); Logger.Instance.Info("Работа закончена за " + stopWatch.Elapsed.ToString(@"mm\:ss\.fff")); Console.WriteLine("Журнал событий сохранен в " + Logger.Instance.GetLogFilePath()); Console.WriteLine("Для завершения нажмите любую клавишу..."); } catch (Exception ex) { Logger.Instance.Fatal("Не удалось получить!", ex); } Console.ReadKey(); }