private void buttonSave_Click(object sender, RoutedEventArgs e) { try { int effected = 0; DbChangeTracker tracker = entity.ChangeTracker; IEnumerable <DbEntityEntry> entries = tracker.Entries(); Console.WriteLine(entity.Employees.Count()); //dataGrid.ItemsSource = entries.ToList(); if (tracker.HasChanges()) { IEnumerable <DbEntityEntry> modifiedEntries = entries.Where(n => n.State == System.Data.Entity.EntityState.Modified); List <Employee> changes = new List <Employee>(); foreach (DbEntityEntry entityEntry in modifiedEntries) { //DbPropertyValues oldValues = entityEntry.GetDatabaseValues(); DbPropertyValues newValues = entityEntry.CurrentValues; Employee employee = (Employee)newValues.ToObject(); changes.Add(employee); } IEnumerable <DbEntityEntry> deletedEntries = entries.Where(n => n.State == System.Data.Entity.EntityState.Deleted || n.State == System.Data.Entity.EntityState.Detached); List <Employee> deletes = new List <Employee>(); foreach (DbEntityEntry entityEntry in deletedEntries) { //DbPropertyValues oldValues = entityEntry.GetDatabaseValues(); DbPropertyValues newValues = entityEntry.CurrentValues; Employee employee = (Employee)newValues.ToObject(); deletes.Add(employee); } IEnumerable <DbEntityEntry> addedEntries = entries.Where(n => n.State == System.Data.Entity.EntityState.Added); List <Employee> adds = new List <Employee>(); foreach (DbEntityEntry entityEntry in addedEntries) { //DbPropertyValues oldValues = entityEntry.GetDatabaseValues(); DbPropertyValues newValues = entityEntry.CurrentValues; Employee employee = (Employee)newValues.ToObject(); adds.Add(employee); } ItemChanges itemChanges = new ItemChanges(); itemChanges.Topmost = true; itemChanges.dataGridChanges.ItemsSource = changes.ToList(); itemChanges.dataGridChangesDelete.ItemsSource = deletes.ToList(); itemChanges.dataGridChangesNew.ItemsSource = adds.ToList(); itemChanges.ShowDialog(); if (itemChanges.IsAccepted) { effected = entity.SaveChanges(); MessageBox.Show("Success to update data: " + effected.ToString() + " item(s)."); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static TicketHistory GenerateTicketHistory(ApplicationDbContext DbContext, Ticket ticket, ApplicationUser user) { // Get the props that have been modified, add to new TicketHistory, return it. DbChangeTracker changeTracker = DbContext.ChangeTracker; var entries = changeTracker.Entries(); var history = new TicketHistory(); history.User = user; if (changeTracker.HasChanges()) { var modifiedEntries = (from prop in entries where prop.State == System.Data.Entity.EntityState.Modified select prop).FirstOrDefault(); var historyDetails = (from propName in modifiedEntries.OriginalValues.PropertyNames let valA = modifiedEntries.OriginalValues[propName]?.ToString() let valB = modifiedEntries.CurrentValues[propName]?.ToString() where valA != valB && (valA == null || !valA.Equals(valB)) && propName != "DateUpdated" select new TicketHistoryDetails() { OldValue = valA, NewValue = valB, Property = propName }).ToList(); history.DateUpdated = (from propName in modifiedEntries.OriginalValues.PropertyNames where propName == "DateUpdated" select(DateTime) modifiedEntries.CurrentValues[propName]).FirstOrDefault(); // Converting Ticket status, priority, and types from their id's to more the more user friendly names. for (int i = 0; i < historyDetails.Count(); i++) { if (historyDetails[i].Property == "TicketStatusId") { var statuses = DbContext.TicketStatuses.ToList(); historyDetails[i].Property = "Ticket Status"; var parsedIdOld = int.Parse(historyDetails[i].OldValue); historyDetails[i].OldValue = statuses.Find(p => p.Id == parsedIdOld).Name; var parsedIdNew = int.Parse(historyDetails[i].NewValue); historyDetails[i].NewValue = statuses.Find(p => p.Id == parsedIdNew).Name; } else if (historyDetails[i].Property == "TicketPriorityId") { var priorities = DbContext.TicketPriorities.ToList(); historyDetails[i].Property = "Ticket Priority"; var parsedIdOld = int.Parse(historyDetails[i].OldValue); historyDetails[i].OldValue = priorities.Find(p => p.Id == parsedIdOld).Name; var parsedIdNew = int.Parse(historyDetails[i].NewValue); historyDetails[i].NewValue = priorities.Find(p => p.Id == parsedIdNew).Name; } else if (historyDetails[i].Property == "TicketTypeId") { var types = DbContext.TicketTypes.ToList(); historyDetails[i].Property = "Ticket Type"; var parsedIdOld = int.Parse(historyDetails[i].OldValue); historyDetails[i].OldValue = types.Find(p => p.Id == parsedIdOld).Name; var parsedIdNew = int.Parse(historyDetails[i].NewValue); historyDetails[i].NewValue = types.Find(p => p.Id == parsedIdNew).Name; } else if (historyDetails[i].Property == "AssignedToId") { var users = DbContext.Users.ToList(); var devUserNew = users.Find(p => p.Id == historyDetails[i].NewValue); var devUserOld = users.Find(p => p.Id == historyDetails[i].OldValue); historyDetails[i].Property = "Dev Assigned"; historyDetails[i].OldValue = devUserOld?.DisplayName; historyDetails[i].NewValue = devUserNew?.DisplayName; if (historyDetails[i].OldValue is null) { historyDetails[i].OldValue = "No dev assigned"; } if (historyDetails[i].NewValue is null) { historyDetails[i].NewValue = "No dev assigned"; } } else if (historyDetails[i].Property == "ProjectId") { var projects = DbContext.Projects.ToList(); var devUserNew = projects.Find(p => p.Id.ToString() == historyDetails[i].NewValue); var devUserOld = projects.Find(p => p.Id.ToString() == historyDetails[i].OldValue); historyDetails[i].Property = "Assigned Project"; historyDetails[i].OldValue = devUserOld.Title; historyDetails[i].NewValue = devUserNew.Title; } } foreach (var detail in historyDetails) { history.TicketHistoryDetails.Add(detail); } } // Ensures an empty history cannot be created. if (history.TicketHistoryDetails.Any()) { return(history); } else { return(null); } }
public bool HasChanges() { return(_changeTracker.HasChanges()); }