public void Import(IImportItems progress, string xmlFilePath) { ImportItems db = new ImportItems(); db.ShowDatabaseLog = DebugMode; progress.OnImportStart(); List<ItemsVM> itemsList = new List<ItemsVM>(); var items = GetItemsFromXML(xmlFilePath); itemsList.AddRange(items); List<String> errors = new List<String>(); db.ShowDatabaseLog = true; db.UpdateAll(itemsList, true, out errors, progress); foreach (var err in errors) { progress.OnError(err); } progress.OnImportFinished(); }
public static void RunImportJob(IImportItems progress, string xmlFilePath) { Importer imp = new Importer(); imp.Import(progress, xmlFilePath); }
public void UpdateAll(List<ItemsVM> items, bool bRemoveObsoleteCars, out List<String> errors, IImportItems progress) { errors = new List<String>(); if (items == null || items.Count == 0) return; _TrackErrors = true; _Errors.Clear(); // cache all. var dbCache = GetDBCache(); var existingItems = dbCache.Items; // This should keep the connecton open _Context.Database.Connection.Open(); int nItemsProcessed = 0; foreach (var item in items) { int existingItemId = 0; var existingItem = existingItems.Find(x => x.ItemRef == item.ItemRef); if (existingItem != null && existingItem.Id > 0) existingItemId = existingItem.Id; try { var result = AddOrUpdate(item, existingItemId, dbCache); if (result == AddUpdateResult.Added) progress.OnAdded(item); else if (result == AddUpdateResult.Updated) progress.OnUpdate(item); else if (result == AddUpdateResult.Error) progress.OnUpdateError(item); ++nItemsProcessed; } catch (Exception /*ex*/) { bool bLog = ShowDatabaseLog; // The context is invalid. recreate it and rebuild cache _Context = new ECommerceDBContext(); ShowDatabaseLog = bLog; _Context.Database.Connection.Open(); dbCache = GetDBCache(); } if (progress != null) { //progress(nItemsProcessed); } // If cars existed, it was updated. So remove it from the list if (existingItem != null && existingItem.Id > 0) existingItems.Remove(existingItem); } if (bRemoveObsoleteCars) { foreach (var item in existingItems) { Remove(item.Id); progress.OnRemoved(item); } } _Context.Database.Connection.Close(); errors = _Errors; _TrackErrors = false; }