private static void RemoveAllIDbInventoryItems(QBPOSEntities db) { foreach (InventoryItem eitm in db.InventoryItems) { db.InventoryItems.Remove(eitm); } }
private void SaveInventoryToDB(TrackableCollection <ItemInventoryRet> trackableCollection) { try { QBPOSEntities db = new QBPOSEntities(); //RemoveAllIDbInventoryItems(db); foreach (ItemInventoryRet itm in trackableCollection) { InventoryItem i = db.InventoryItems.FirstOrDefault(x => x.ListID == itm.ListID); if (i == null) { i = new InventoryItem(); db.InventoryItems.Add(i); } i.ALU = itm.ALU; i.Attribute = itm.Attribute; i.DepartmentCode = itm.DepartmentCode; i.ItemDesc1 = itm.Desc1; i.ItemDesc2 = itm.Desc2; i.ItemNumber = System.Convert.ToInt32(itm.ItemNumber); i.ItemType = itm.ItemType; i.ListID = itm.ListID; i.Price = Convert.ToDouble(itm.Price1); i.Size = itm.Size; i.TaxCode = itm.TaxCode; i.UnitOfMeasure = itm.UnitOfMeasure; } try { db.SaveChanges(); System.Windows.MessageBox.Show("Inventory Items Import Complete"); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine( "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } catch (Exception ex) { throw ex; } }