public void Handle(SavePicture notification) { if (!notification.HasImage()) { throw new InvalidOperationException("No Image was uploaded."); } StudentPicture studentPicture = _dbContext.StudentPictures .Where(e => e.StudentId.Equals(_currentUser.User.Id)) .SingleOrDefault(e => e.ImageType == notification.Type) ?? new StudentPicture { ImageType = notification.Type }; studentPicture.StudentId = _currentUser.User.Id; studentPicture.DateSubmitted = DateTime.Now; studentPicture.ImageName = notification.FileName; studentPicture.ImageData = notification.ImageData; studentPicture.MimeType = notification.MimeType; if (studentPicture.Id == default(int)) { _dbContext.StudentPictures.Add(studentPicture); } else { ObjectStateManager objectStateManager = ((IObjectContextAdapter)_dbContext).ObjectContext.ObjectStateManager; _dbContext.StudentPictures.Attach(studentPicture); objectStateManager.ChangeObjectState(studentPicture, EntityState.Modified); } _dbContext.SaveChanges(); }
/// <summary> /// откатывам все изменения в контексте, /// </summary> public void Rollback() { ServerAction = true; //detaching all added entities var addedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Added); foreach (var objectStateEntry in addedEntities) { Detach(objectStateEntry.Entity); } var deletedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Deleted); //apply origin value for all modified entities foreach (var objectStateEntry in deletedEntities) { if (objectStateEntry.Entity != null) { ObjectStateManager.ChangeObjectState(objectStateEntry.Entity, EntityState.Modified); } } //apply origin value for all modified entities var modifiedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified); foreach (var objectStateEntry in modifiedEntities) { RevertEntityScalars((EntityObject)objectStateEntry.Entity); } //set state for every changed entities in unchanged AcceptAllChanges(); ServerAction = false; }
public void Handle(SaveExpression notification) { StudentExpression studentExpression = _dbContext.StudentExpressions .Where(e => e.StudentId.Equals(notification.StudentId)) .SingleOrDefault(e => e.Type == notification.Type) ?? new StudentExpression { Type = notification.Type }; studentExpression.StudentId = notification.StudentId; studentExpression.Text = notification.Text; if (studentExpression.Id == default(int)) { _dbContext.StudentExpressions.Add(studentExpression); } else { ObjectStateManager objectStateManager = ((IObjectContextAdapter)_dbContext).ObjectContext.ObjectStateManager; _dbContext.StudentExpressions.Attach(studentExpression); objectStateManager.ChangeObjectState(studentExpression, EntityState.Modified); } _dbContext.SaveChanges(); }
public override void OnBeforeUpdate(DbContext dbContext, ObjectStateManager manager, IVersion item) { if (item.VersionNo == 0) { throw new Exception("Unexpected , version number should not be zero while update."); } base.OnBeforeUpdate(dbContext, manager, item); item.VersionNo = item.VersionNo + 1; manager.ChangeObjectState(item, EntityState.Added); }
public override void OnBeforeDelete(DbContext dbContext, ObjectStateManager manager, ISoftDelete item) { if (item.IsDeleted) { throw new InvalidOperationException("Item is already deleted."); } base.OnBeforeDelete(dbContext, manager, item); dbContext.Entry(item).Reload(); item.IsDeleted = true; manager.ChangeObjectState(item, EntityState.Modified); }
public void Handle(SaveGradInfo notification) { GraduateInformation information = _dbContext.GraduateInformation .SingleOrDefault(i => i.StudentId == _currentUser.User.Id) ?? new GraduateInformation { StudentId = _currentUser.User.Id }; information.Name = notification.Name; information.Street = notification.Street; information.City = notification.City; information.StudentEmail = notification.StudentEmail; information.ParentEmail = notification.ParentEmail; information.FineArts = notification.FineArts; information.AcademicClasses = notification.AcademicClasses; information.WillParticipateInGraduation = notification.WillParticipateInGraduation; information.TakenKeysWorldView = notification.TakenKeysWorldView; information.TakenApprovedWorldView = notification.TakenApprovedWorldView; information.WillSecureAnnouncements = notification.WillSecureAnnouncements; information.NeedCapAndGown = notification.NeedCapAndGown; information.Height = notification.Height; if (information.Id == default(int)) { _dbContext.GraduateInformation.Add(information); } else { ObjectStateManager objectStateManager = ((IObjectContextAdapter)_dbContext).ObjectContext.ObjectStateManager; _dbContext.GraduateInformation.Attach(information); objectStateManager.ChangeObjectState(information, EntityState.Modified); } _dbContext.SaveChanges(); }
public void ChangeState <T>(ObjectState state, T entity) where T : class { ObjectStateManager.ChangeObjectState(entity, StateHelpers.GetEquivalentEntityState(state)); }
public void ChangeObjectState(Object entity, EntityState entityState) { ObjectStateManager.ChangeObjectState(entity, entityState); }
private static void ChangeStateOfEntity(ObjectStateManager objectStateManager, EntityWithId entity) { // TODO: Check for changs or deleted before moving this to Added/Modified objectStateManager.ChangeObjectState(entity, entity.Id == 0 ? EntityState.Added : EntityState.Modified); }
public void SetModified(object entity) { ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); }