public static int[] UpdatePage(List<ToDoView> itemsI, List<ToDoView> itemsM, List<int> toDelete) { if (itemsM == null && itemsI == null && toDelete == null) return new int[0]; List<ToDo> inserted = new List<ToDo>(); using (SiteDbEntities context = new SiteDbEntities()) { bool aChange = false; if (itemsI != null) { foreach (ToDoView item in itemsI) { ToDo curr = new ToDo() { Name = item.Name, Description = item.Description, DueDate = item.DueDate }; aChange = true; context.ToDo.AddObject(curr); inserted.Add(curr); } } if (itemsM != null) { foreach (ToDoView item in itemsM) { ToDo curr = new ToDo() { Name = item.Name, Description = item.Description, DueDate = item.DueDate, id = item.id.Value }; context.ToDo.Attach(curr); context.ObjectStateManager.ChangeObjectState(curr, System.Data.EntityState.Modified); aChange = true; } } if (toDelete != null) { foreach (int key in toDelete) { ToDo curr = new ToDo() { id = key }; context.ToDo.Attach(curr); context.ObjectStateManager.ChangeObjectState(curr, System.Data.EntityState.Deleted); aChange = true; } } if (aChange) { try { context.SaveChanges(); } catch { } return inserted.Select(m => m.id).ToArray(); } else return new int[0]; } }
public static int[] UpdatePageTasks(List<SubTasksView> itemsI, List<SubTasksView> itemsM, List<int> toDelete) { if (itemsM == null && itemsI == null && toDelete == null) return new int[0]; List<SubTasks> inserted = new List<SubTasks>(); using (SiteDbEntities context = new SiteDbEntities()) { bool aChange = false; if (itemsI != null) { foreach (SubTasksView item in itemsI) { SubTasks curr = new SubTasks() { Name = item.Name, idTask = item.FatherId.Value, WorkingDays = item.WorkingDays }; aChange = true; context.SubTasks.AddObject(curr); inserted.Add(curr); } } if (itemsM != null) { foreach (SubTasksView item in itemsM) { SubTasks curr = new SubTasks() { Name = item.Name, code = item.Key.Value, idTask=item.FatherId.Value, WorkingDays=item.WorkingDays}; context.SubTasks.Attach(curr); context.ObjectStateManager.ChangeObjectState(curr, System.Data.EntityState.Modified); aChange = true; } } if (toDelete != null) { foreach (int key in toDelete) { SubTasks curr = new SubTasks() { code = key }; context.SubTasks.Attach(curr); context.ObjectStateManager.ChangeObjectState(curr, System.Data.EntityState.Deleted); aChange = true; } } if (aChange) { try { context.SaveChanges(); } catch { } return inserted.Select(m => m.code).ToArray(); } else return new int[0]; } }