/// <summary> /// Refreshes all pending Delete/Update entity objects of current DataContext according to the specified mode. /// Nothing will do on Pending Insert entity objects. /// </summary> /// <param name="context"></param> /// <param name="refreshMode">A value that specifies how optimistic concurrency conflicts are handled.</param> public void RefreshPendingChanges(DataContext context, RefreshMode refreshMode) { ChangeSet changeSet = context.GetChangeSet(); if (changeSet != null) { context.Refresh(refreshMode, changeSet.Deletes); context.Refresh(refreshMode, changeSet.Updates); } }
public int Save(object dataToSave) { var changeSet = _dataContext.GetChangeSet(); _dataContext.SubmitChanges(); return(changeSet.Inserts.Count + changeSet.Updates.Count); }
public static void RollBack(this DataContext dataContext) { ChangeSet pendingChanges = dataContext.GetChangeSet(); foreach (var insertion in pendingChanges.Inserts) { dataContext.GetTable(insertion.GetType()).InsertOnSubmit(insertion); } foreach (var deletion in pendingChanges.Deletes) { dataContext.GetTable(deletion.GetType()).DeleteOnSubmit(deletion); } foreach (var update in pendingChanges.Updates) { dataContext.Refresh(RefreshMode.OverwriteCurrentValues, update); IPropertyChangedNotifier updateNotify = update as IPropertyChangedNotifier; foreach (PropertyInfo propertyInfo in update.GetType().GetProperties()) { updateNotify.NotifyPropertyChanged(propertyInfo.Name); } } }
/// <summary> /// Iterates through the collection of MvvmBinders to see if any ViewModels are dirty. /// </summary> /// <returns></returns> public bool AnyDirty() { foreach (IMvvmBinder b in OpenMvvmBinders) { try { DataContext dc = b.DataContext as DataContext; // MainViewModel will always be null, so we ignore it..... if (null != dc) { ChangeSet cs = dc.GetChangeSet(); if (0 != cs.Updates.Count || 0 != cs.Inserts.Count || 0 != cs.Deletes.Count) { return(true); } } } catch (Exception ex) { MessageBox.Show("Error in Host: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } return(false); }
public static int SaveChanges(this DataContext dataContext) { ChangeSet changeSet = dataContext.GetChangeSet(); int numberOfAffectedEntities = changeSet.Inserts.Count + changeSet.Updates.Count + changeSet.Deletes.Count; dataContext.SubmitChanges(); return(numberOfAffectedEntities); }
internal DisconnectedChangeSet(DataContext dataContext) { // Obtenemos el ChangeSet del DataContext Dictionary <object, object> listaEntidades = new Dictionary <object, object>(); ChangeSet changeSet = dataContext.GetChangeSet(); _modificados = new List <object>(changeSet.Updates); _eliminados = new List <object>(changeSet.Deletes); _agregados = new List <object>(changeSet.Inserts); // Obtenemos los valores originales int i; for (i = 0; i < _modificados.Count; i++) { _modificadosOriginales.Add(OperacionesEntidad.ObtenerEntidadOriginal(dataContext, _modificados[i])); } for (i = 0; i < _eliminados.Count; i++) { _eliminados[i] = OperacionesEntidad.ObtenerEntidadOriginal(dataContext, _eliminados[i]); } // Creamos copias para no serializar las relaciones for (i = 0; i < _modificados.Count; i++) { _modificados[i] = OperacionesEntidad.CreateDataCopy(_modificados[i], true); listaEntidades.Add(changeSet.Updates[i], _modificados[i]); } for (i = 0; i < _eliminados.Count; i++) { _eliminados[i] = OperacionesEntidad.CreateDataCopy(_eliminados[i], true); listaEntidades.Add(changeSet.Deletes[i], _eliminados[i]); } for (i = 0; i < _agregados.Count; i++) { _agregados[i] = OperacionesEntidad.CreateDataCopy(_agregados[i], true); listaEntidades.Add(changeSet.Inserts[i], _agregados[i]); } for (i = 0; i < _modificadosOriginales.Count; i++) { _modificadosOriginales[i] = OperacionesEntidad.CreateDataCopy(_modificadosOriginales[i], true); } // Actualizamos asociaciones a copias for (i = 0; i < _modificados.Count; i++) { OperacionesEntidad.ActualizarAsociaciones(changeSet.Updates[i], _modificados[i], listaEntidades); } for (i = 0; i < _eliminados.Count; i++) { OperacionesEntidad.ActualizarAsociaciones(changeSet.Deletes[i], _eliminados[i], listaEntidades); } for (i = 0; i < _agregados.Count; i++) { OperacionesEntidad.ActualizarAsociaciones(changeSet.Inserts[i], _agregados[i], listaEntidades); } }
private static void ProcessDeleteItems(DataContext _dataContext, DataContext _historyDataContext, int _userID) { ChangeSet CurrentChangeSet = _dataContext.GetChangeSet(); foreach (var item in CurrentChangeSet.Deletes) { CopyTableColumns(item, CommitActionType.Delete, _historyDataContext, _userID); } }
private static void Flush(DataContext context) { var changeSet = context.GetChangeSet(); changeSet.Inserts.OfType<ISavingChangesEventHandler>().ToList().ForEach(x => x.OnInsert()); changeSet.Updates.OfType<ISavingChangesEventHandler>().ToList().ForEach(x => x.OnUpdate()); changeSet.Deletes.OfType<ISavingChangesEventHandler>().ToList().ForEach(x => x.OnDelete()); context.SubmitChanges(); }
public bool TryGetObjectStateEntry(object entity, out ObjectStateEntry entry) { Type entityType = entity.GetType(); var primaryKeys = DataContextHelper.GetPrimaryKeys(entityType); EntityKey key = new EntityKey(entityType, primaryKeys.Select(x => new KeyValuePair <Property, object>(x, x.GetValue(entity)))); ChangeSet changeSet = _context.GetChangeSet(); if (changeSet.Inserts.Any(x => x == entity)) { entry = new ObjectStateEntry { EntityKey = key, Entity = entity, State = EntityState.Added }; return(true); } if (changeSet.Updates.Any(x => x == entity)) { entry = new ObjectStateEntry { EntityKey = key, Entity = entity, State = EntityState.Modified }; return(true); } if (changeSet.Deletes.Any(x => x == entity)) { entry = new ObjectStateEntry { EntityKey = key, Entity = entity, State = EntityState.Deleted }; return(true); } MetaType metaType = _context.Mapping.GetTable(key.EntityType).RowType; if (_dataServices.IsCachedObject(metaType, entity)) { entry = new ObjectStateEntry { EntityKey = key, Entity = entity, State = EntityState.Unchanged }; return(true); } entry = null; return(false); }
private static void Flush(DataContext context) { var changeSet = context.GetChangeSet(); changeSet.Inserts.OfType <ISavingChangesEventHandler>().ToList().ForEach(x => x.OnInsert()); changeSet.Updates.OfType <ISavingChangesEventHandler>().ToList().ForEach(x => x.OnUpdate()); changeSet.Deletes.OfType <ISavingChangesEventHandler>().ToList().ForEach(x => x.OnDelete()); context.SubmitChanges(); }
/// <summary> /// Refreshes all pending Delete/Update entity objects of current DataContext according to the specified mode. /// Nothing will do on Pending Insert entity objects. /// </summary> /// <param name="context"></param> /// <param name="refreshMode">A value that specifies how optimistic concurrency conflicts are handled.</param> public static void RefreshPendingChanges(this DataContext context, RefreshMode refreshMode) { ChangeSet changeSet = context.GetChangeSet(); if (changeSet != null) { context.Refresh(refreshMode, changeSet.Deletes); context.Refresh(refreshMode, changeSet.Updates); } }
/// <summary> /// Creates the <see cref="AuditLog"/> of changes form the specified <see cref="DataContext"/>. /// </summary> /// <param name="dataContext">The <see cref="DataContext"/> to get the changes from.</param> /// <returns>An instance of <see cref="AuditLog"/> that is an audit log of the changes to <see cref="DataContext"/>.</returns> /// <remarks> /// An entity has to be marked with the <see cref="AuditAttribute"/> for audit data to be collected for that entity. /// </remarks> public static AuditLog CreateAuditLog(DataContext dataContext) { if (dataContext == null) { throw new ArgumentNullException("dataContext"); } ChangeSet changeSet = dataContext.GetChangeSet(); return(CreateAuditLog(dataContext, changeSet)); }
public static bool HasPendingChanges(this DataContext dataContext) { ChangeSet pendingChanges = dataContext.GetChangeSet(); if (pendingChanges.Deletes.Count > 0 || pendingChanges.Inserts.Count > 0 || pendingChanges.Updates.Count > 0) { return(true); } return(false); }
private static void ProcessUpdateItems(DataContext _dataContext, DataContext _historyDataContext, int _userID) { ChangeSet CurrentChangeSet = _dataContext.GetChangeSet(); foreach (object item in CurrentChangeSet.Updates) { ITable table = _dataContext.GetTable(item.GetType()); object originalItem = table.GetOriginalEntityState(item); CommitActionType actionType = CommitActionType.Update; CopyTableColumns(originalItem, actionType, _historyDataContext, _userID); } }
private static void CheckForInvalidVersions(DataContext _dataContext) { var changes = _dataContext.GetChangeSet(); foreach (object item in changes.Updates) { ITable table = _dataContext.GetTable(item.GetType()); object originalItem = table.GetOriginalEntityState(item); System.Data.Linq.Binary originalVerCol = (System.Data.Linq.Binary)originalItem.GetPropertyValue("VerCol"); System.Data.Linq.Binary currentVerCol = (System.Data.Linq.Binary)item.GetPropertyValue("VerCol"); if (!System.Data.Linq.Binary.Equals(currentVerCol, originalVerCol)) { throw new ChangeConflictException(string.Format("{0} object instance with ID ({1}) being modified with rowversion({2}) has different rowversion({3}) in database.", item.GetType().Name, item.GetPropertyValue("ID"), currentVerCol, originalVerCol)); } } }
public void setUpdateColumns() { var changes = dataContext.GetChangeSet(); foreach (object item in changes.Updates) { setIntProperty(item, UpdateUserID, userId); setDateTimeProperty(item, UpdateDate, DateTime.UtcNow); } foreach (object item in changes.Inserts) { setIntProperty(item, UpdateUserID, userId); setDateTimeProperty(item, UpdateDate, DateTime.UtcNow); setIntProperty(item, InsertUserID, userId); setDateTimeProperty(item, InsertDate, DateTime.UtcNow); } }
public void DiscardPendingChanges(DataContext context) { RefreshPendingChanges(context, RefreshMode.OverwriteCurrentValues); ChangeSet changeSet = context.GetChangeSet(); if (changeSet != null) { //Undo inserts foreach (object objToInsert in changeSet.Inserts) { context.GetTable(objToInsert.GetType()).DeleteOnSubmit(objToInsert); if (objToInsert is NearBy) { (objToInsert as NearBy).Tour.NearBy.Remove((objToInsert as NearBy)); } else if (objToInsert is Repair) { (objToInsert as Repair).Tour.Repair.Remove((objToInsert as Repair)); } else if (objToInsert is PictureTour) { (objToInsert as PictureTour).Tour.Pictures.Remove((objToInsert as PictureTour)); } } //Undo deletes foreach (object objToDelete in changeSet.Deletes) { context.GetTable(objToDelete.GetType()).InsertOnSubmit(objToDelete); if (objToDelete is NearBy) { (objToDelete as NearBy).Tour.NearBy.Add((objToDelete as NearBy)); } else if (objToDelete is Repair) { (objToDelete as Repair).Tour.Repair.Add((objToDelete as Repair)); } else if (objToDelete is PictureTour) { (objToDelete as PictureTour).Tour.Pictures.Add((objToDelete as PictureTour)); } } } }
/// <summary> /// Run the rules for the the changed objects in the <see cref="DataContext"/>. /// </summary> /// <param name="dataContext">The <see cref="DataContext"/> to get the <see cref="ChangeSet"/> from and run rules against.</param> /// <returns><c>true</c> if rules ran successfully; otherwise, <c>false</c>.</returns> public bool Run(DataContext dataContext) { ChangeSet changeSet = dataContext.GetChangeSet(); var tracked = new List <TrackedObject>(); foreach (object o in changeSet.Inserts) { tracked.Add(new TrackedObject { Current = o, IsNew = true }); } foreach (object o in changeSet.Deletes) { tracked.Add(new TrackedObject { Current = o, IsDeleted = true }); } foreach (object o in changeSet.Updates) { var trackedObject = new TrackedObject { Current = o, IsChanged = true }; if (o != null) { var metaType = dataContext.Mapping.GetMetaType(o.GetType()); var type = metaType.InheritanceRoot == null ? metaType.Type : metaType.InheritanceRoot.Type; ITable table = dataContext.GetTable(type); trackedObject.Original = table.GetOriginalEntityState(o); } tracked.Add(trackedObject); } return(Run(tracked)); }
/// <summary> /// Discard all pending changes of current DataContext. /// All un-submitted changes, including insert/delete/modify will lost. /// </summary> /// <param name="context"></param> public static void DiscardPendingChanges(this DataContext context) { context.RefreshPendingChanges(RefreshMode.OverwriteCurrentValues); ChangeSet changeSet = context.GetChangeSet(); if (changeSet != null) { //Undo inserts foreach (object objToInsert in changeSet.Inserts) { context.GetTable(objToInsert.GetType()).DeleteOnSubmit(objToInsert); } //Undo deletes foreach (object objToDelete in changeSet.Deletes) { context.GetTable(objToDelete.GetType()).InsertOnSubmit(objToDelete); } } }
public static void MakeUpdatesDirty( this DataContext dataContext, params string[] members) { if (dataContext == null) { throw new ArgumentNullException("dataContext"); } if (members == null) { throw new ArgumentNullException("members"); } if (members.Length == 0) { return; // nothing to do } foreach (object instance in dataContext.GetChangeSet().Updates) { MakeDirty(dataContext, instance, members); } }
public bool IsValidChangeSet(DataContext context, List <int> permittedIds, out string error) { var changes = context.GetChangeSet(); var updates = new Dictionary <object, ShadowModifiedMember[]>(); foreach (var m in changes.Updates) { var modified = context.GetTable(m.GetType()).GetModifiedMembers(m); updates[m] = modified.Select( x => new ShadowModifiedMember { Member = x.Member, CurrentValue = x.CurrentValue, OriginalValue = x.OriginalValue } ).ToArray(); } return(IsValidChangeSet(updates, changes.Deletes, changes.Inserts, changes.Updates, permittedIds, out error)); }
/// <summary> /// Tells whether Database has changed indicating to trigger refresh all in memory objects /// </summary> /// <returns>true if database changed</returns> public static bool HasDbChanged(DataContext db) { ChangeSet changes = db.GetChangeSet(); return (changes.Updates.Count > 0 || changes.Inserts.Count > 0 || changes.Deletes.Count > 0); }
protected override PrincipalInvestigator GetPrincipalInvestigator(int principalInvestigatorId) { return(DataContext.PrincipalInvestigators.FirstOrDefault(it => it.PrincipalInvestigatorId == principalInvestigatorId) ?? DataContext.GetChangeSet().Inserts.FirstOrDefault(it => it is PrincipalInvestigator && (it as PrincipalInvestigator).PrincipalInvestigatorId == principalInvestigatorId) as PrincipalInvestigator); }
public static IEnumerable <TEntity> GetDeletes <TEntity>(this DataContext dataContext) where TEntity : class { var changeSet = dataContext.GetChangeSet(); return(changeSet.Deletes.Where(update => update.GetType() == typeof(TEntity)).Select(update => update as TEntity)); }
public void GetChangeSet() { // TODO context.GetChangeSet(); }
//private static void ProcessInsertItems(DataContext _dataContext, DataContext _historyDataContext, int _userID) //{ // if (EntitiesToInsert.Count == 0) // return; // foreach (var item in EntitiesToInsert) // { // CopyTableColumns(item, CommitActionType.Insert, _historyDataContext, _userID); // } //} private static void ProcessUpdateItems(DataContext _dataContext, DataContext _historyDataContext, int _userID) { ChangeSet CurrentChangeSet = _dataContext.GetChangeSet(); foreach (object item in CurrentChangeSet.Updates) { ITable table = _dataContext.GetTable(item.GetType()); object originalItem = table.GetOriginalEntityState(item); CommitActionType actionType = CommitActionType.Update; CopyTableColumns(originalItem, actionType, _historyDataContext, _userID); } }