public bool SaveItem(DataModel.Common.IDataStoreItem item) { lock (thisLock) { try { if (item == null) { logger.Error("There was an attempt to save a null item"); return(false); } if (item.ID == 0) { item.CreationDate = DateTime.Now; item.LastEditDate = item.CreationDate; item.Creator = Platform.CurrentUser; db.Entry(item).State = EntityState.Added; //db.Files.Add(item as File); List <DbEntityEntry> modifiedChanges = db.ChangeTracker.Entries().Where(x => x.State == EntityState.Modified).ToList(); int numChanges = modifiedChanges.Count; db.SaveChanges(); _itemsSaved++; _itemsSaved = _itemsSaved + numChanges; } else if (DateTime.Compare(db.Entry(item).GetDatabaseValues().GetValue <DateTime>("LastEditDate"), item.LastEditDate) != 0) { logger.Error("Throw concurrency Error db.LastEditDate = " + db.Entry(item).GetDatabaseValues().GetValue <DateTime>("LastEditDate") + " item.LastEditDate = " + item.LastEditDate); db.Entry(item).State = EntityState.Modified; db.Entry(item).Reload(); throw new Exception("Concurrency Error, reloaded from database"); } else { item.LastEditDate = DateTime.Now; List <DbEntityEntry> modifiedChanges = db.ChangeTracker.Entries().Where(x => x.State == EntityState.Modified).ToList(); int numChanges = modifiedChanges.Count; //AddDataStoreItemAlterations(item); db.SaveChanges(); _itemsSaved = _itemsSaved + numChanges; } } catch (Exception ex) { logger.Error("Error during SaveItem() " + ex.Message); while (ex.InnerException != null) { ex = ex.InnerException; logger.Error("...Inner exception: " + ex.Message); } var myException = new Exception("SaveItem in the dataretriever failed to save: " + ex.Message, ex); throw myException; } return(true); } }
public bool SaveItem(DataModel.Common.IDataStoreItem item) { lock (thisLock) { try { if (item == null) { } else { } if (item.ID == 0) { logger.Trace("Item is being added"); item.CreationDate = DateTime.Now; db.Entry(item).State = EntityState.Added; db.SaveChanges(); } else if (DateTime.Compare(db.Entry(item).GetDatabaseValues().GetValue <DateTime>("LastEditDate"), item.LastEditDate) != 0) { logger.Error("Throw concurrency Error db.LastEditDate = " + db.Entry(item).GetDatabaseValues().GetValue <DateTime>("LastEditDate") + " item.LastEditDate = " + item.LastEditDate); db.Entry(item).State = EntityState.Modified; //throw new Exception("Concurrency Error"); item.LastEditDate = DateTime.Now; db.SaveChanges(); } else { //Here we would implement all the changetracking. //we iterate through all properties i guess.... logger.Trace("Item.ID != 0 and lastEditDate checks out..."); db.Entry(item).State = EntityState.Modified; item.LastEditDate = DateTime.Now; AddDataStoreItemAlterations(item); UpdateLinkedItems(item); db.SaveChanges(); } } catch (DbUpdateConcurrencyException ex) { Console.WriteLine("...Caught Exception " + ex.Message); logger.Error("Error during SaveItem() " + ex.Message); } return(true); } }
public DataModel.Common.IDataStoreItem RetrieveItem(DataModel.Common.IDataStoreItem item) { if (item.ID != null) { int id = item.ID; return(RetrieveItem(id, item.GetType())); } else { throw new Exception("Couldn't find that item in the database"); } }