public static void Insert(EmployeeDirectoryModel employee)
        {
            if (!UpdateDatabase)
            {
                var first = All().OrderByDescending(e => e.EmployeeId).FirstOrDefault();

                var id = 0;

                if (first != null)
                {
                    id = first.EmployeeId;
                }

                employee.EmployeeId = id + 1;

                All().Insert(0, employee);
            }
            else
            {
                using (var db = new SampleEntities())
                {                    
                    var entity = employee.ToEntity();

                    db.EmployeeDirectory.AddObject(entity);
                    db.SaveChanges();

                    employee.EmployeeId = entity.EmployeeID;
                }
            }
        }
        public static void Insert(GanttTaskModel task)
        {
            if (!UpdateDatabase)
            {
                var first = All().OrderByDescending(e => e.ID).FirstOrDefault();

                var id = 0;

                if (first != null)
                {
                    id = first.ID;
                }

                task.ID = id + 1;

                All().Insert(0, task);
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = task.ToEntity();

                    db.GanttTasks.AddObject(entity);

                    db.SaveChanges();

                    task.ID = entity.ID;
                }
            }
        }
        public static void Insert(GanttResourceAssignmentModel assignment)
        {
            if (!UpdateDatabase)
            {
                var first = All().OrderByDescending(a => a.ID).FirstOrDefault();
                var id = 0;

                if (first != null)
                {
                    id = first.ID;
                }

                assignment.ID = id + 1;

                All().Add(assignment);
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = assignment.ToEntity();

                    db.GanttResourceAssignments.AddObject(entity);

                    db.SaveChanges();
                }
            }
        }
        public static void Insert(OrgChartShape shape)
        {
            if (!UpdateDatabase)
            {
                var first = All().OrderByDescending(e => e.Id).FirstOrDefault();

                var id = 0;

                if (first != null)
                {
                    id = first.Id;
                }

                shape.Id = id + 1;

                All().Insert(0, shape);
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    db.OrgChartShapes.AddObject(shape);
                    db.SaveChanges();
                }
            }
        }
        public static void Update(MeetingViewModel meeting)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.MeetingID == meeting.MeetingID);

                if (target != null)
                {
                    target.Title = meeting.Title;
                    target.Start = meeting.Start;
                    target.End = meeting.End;
                    target.StartTimezone = meeting.StartTimezone;
                    target.EndTimezone = meeting.EndTimezone;
                    target.Description = meeting.Description;
                    target.IsAllDay = meeting.IsAllDay;
                    target.RecurrenceRule = meeting.RecurrenceRule;
                    target.RoomID = meeting.RoomID;
                    target.RecurrenceException = meeting.RecurrenceException;
                    target.RecurrenceID = meeting.RecurrenceID;
                    target.Attendees = meeting.Attendees;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    if (meeting.Attendees == null)
                    {
                        meeting.Attendees = new int[0];
                    }
                    
                    var entity = meeting.ToEntity();

                    db.Meetings.Attach(entity);

                    var attendees = meeting.Attendees.Select(attendee => new MeetingAttendee
                    {
                        AttendeeID = attendee
                    });

                    foreach (var attendee in attendees)
                    {
                        db.MeetingAttendees.Attach(attendee);
                    }

                    entity.MeetingAttendees.Clear();

                    foreach (var attendee in attendees)
                    {
                        entity.MeetingAttendees.Add(attendee);
                    }

                    db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
                    db.SaveChanges();
                }
            }
        }
        public static void Insert(MeetingViewModel meeting)
        {
            if (!UpdateDatabase)
            {
                var first = All().OrderByDescending(e => e.MeetingID).FirstOrDefault();

                var id = 0;

                if (first != null)
                {
                    id = first.MeetingID;
                }                               

                meeting.MeetingID = id + 1;                

                All().Insert(0, meeting);
            } 
            else 
            {
                using (var db = new SampleEntities())
                {
                    if (meeting.Attendees == null)
                    {
                        meeting.Attendees = new int[0];
                    }                    

                    var entity = meeting.ToEntity();

                    foreach (var attendeeId in meeting.Attendees)
                    {
                        entity.MeetingAttendees.Add(new MeetingAttendee
                        {
                            AttendeeID = attendeeId
                        });
                    }

                    db.Meetings.AddObject(entity);
                    db.SaveChanges();

                    meeting.MeetingID = entity.MeetingID;
                }
            }
        }        
        public static void Delete(MeetingViewModel meeting)
        {
            if (!UpdateDatabase)
            {
                var target = One(p => p.MeetingID == meeting.MeetingID);
                if (target != null)
                {
                    All().Remove(target);

                    var recurrenceExceptions = All().Where(m => m.RecurrenceID == meeting.MeetingID).ToList();

                    foreach (var recurrenceException in recurrenceExceptions)
                    {
                        All().Remove(recurrenceException);
                    }
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    if (meeting.Attendees == null)
                    {
                        meeting.Attendees = new int[0];
                    }

                    var entity = meeting.ToEntity();

                    db.Meetings.Attach(entity);

                    var attendees = meeting.Attendees.Select(attendee => new MeetingAttendee
                    {
                        AttendeeID = attendee
                    });

                    foreach (var attendee in attendees)
                    {
                        db.MeetingAttendees.Attach(attendee);
                    }

                    entity.MeetingAttendees.Clear();

                    var recurrenceExceptions = db.Meetings.Where(m => m.RecurrenceID == entity.MeetingID);

                    foreach (var recurrenceException in recurrenceExceptions)
                    {
                        db.Meetings.DeleteObject(recurrenceException);
                    }

                    db.Meetings.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
        }
        public static void Update(GanttTaskModel task)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.ID == task.ID);

                if (target != null)
                {
                    target.Title = task.Title;
                    target.Start = task.Start;
                    target.End = task.End;
                    target.PercentComplete = task.PercentComplete;
                    target.OrderID = task.OrderID;
                    target.ParentID = task.ParentID;
                    target.Summary = task.Summary;
                    target.Expanded = task.Expanded;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = task.ToEntity();

                    db.GanttTasks.Attach(entity);
                    db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

                    db.SaveChanges();
                }
            }
        }
        public static void Delete(TaskViewModel task)
        {
            if (!UpdateDatabase)
            {
                var target = One(p => p.TaskID == task.TaskID);
                if (target != null)
                {
                    All().Remove(target);

                    var recurrenceExceptions = All().Where(m => m.RecurrenceID == task.TaskID).ToList();

                    foreach (var recurrenceException in recurrenceExceptions)
                    {
                        All().Remove(recurrenceException);
                    }
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = task.ToEntity();
                    db.Tasks.Attach(entity);

                    var recurrenceExceptions = db.Tasks.Where(t => t.RecurrenceID == task.TaskID);

                    foreach (var recurrenceException in recurrenceExceptions)
                    {
                        db.Tasks.DeleteObject(recurrenceException);
                    }

                    db.Tasks.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
        }
        public static void Update(GanttDependencyModel dependency)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.ID == dependency.ID);

                if (target != null)
                {
                    target.Type = dependency.Type;
                    target.PredecessorID = dependency.PredecessorID;
                    target.SuccessorID = dependency.SuccessorID;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = dependency.ToEntity();

                    db.GanttDependencies.Attach(entity);
                    db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

                    db.SaveChanges();
                }
            }
        }
        public static void Update(GanttResourceAssignmentModel assigment)
        {
            if (!UpdateDatabase)
            {
                var target = One(a => a.ID == assigment.ID);

                if (target != null)
                {
                    target.Units = assigment.Units;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = assigment.ToEntity();

                    db.GanttResourceAssignments.Attach(entity);
                    db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);

                    db.SaveChanges();
                }
            }
        }
        public static void Delete(GanttResourceAssignmentModel assigment)
        {
            if (!UpdateDatabase)
            {
                var target = One(a => a.ID == assigment.ID);
                if (target != null)
                {
                    All().Remove(target);
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = assigment.ToEntity();
                    db.GanttResourceAssignments.Attach(entity);

                    db.GanttResourceAssignments.DeleteObject(entity);

                    db.SaveChanges();
                }
            }
        }
        public static void Update(EmployeeDirectoryModel employee)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.EmployeeId == employee.EmployeeId);

                if (target != null)
                {
                    target.FirstName = employee.FirstName;
                    target.LastName = employee.LastName;
                    target.Address = employee.Address;
                    target.City = employee.City;
                    target.Country = employee.Country;
                    target.Phone = employee.Phone;
                    target.Extension = employee.Extension;
                    target.BirthDate = employee.BirthDate;
                    target.HireDate = employee.HireDate;
                    target.Position = employee.Position;
                    target.ReportsTo = employee.ReportsTo;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = employee.ToEntity();
                    db.EmployeeDirectory.Attach(entity);
                    db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
                    db.SaveChanges();
                }
            }
        }
        public static void Update(OrgChartConnection connection)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.Id == connection.Id);

                if (target != null)
                {
                    target.FromShapeId = connection.FromShapeId;
                    target.ToShapeId = connection.ToShapeId;
                    target.Text = connection.Text;
                    target.FromPointX = connection.FromPointX;
                    target.FromPointY = connection.FromPointY;
                    target.ToPointX = connection.ToPointX;
                    target.ToPointY = connection.ToPointY;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    db.OrgChartConnections.Attach(connection);
                    db.ObjectStateManager.ChangeObjectState(connection, EntityState.Modified);
                    db.SaveChanges();
                }
            }
        }
        public static void Update(TaskViewModel task)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.TaskID == task.TaskID);

                if (target != null)
                {
                    target.Title = task.Title;
                    target.Description = task.Description;
                    target.IsAllDay = task.IsAllDay;
                    target.RecurrenceRule = task.RecurrenceRule;
                    target.RecurrenceException = task.RecurrenceException;
                    target.RecurrenceID = task.RecurrenceID;
                    target.OwnerID = task.OwnerID;
                    target.StartTimezone = task.StartTimezone;
                    target.EndTimezone = task.EndTimezone;
                    target.Start = task.Start;
                    target.End = task.End;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = task.ToEntity();
                    db.Tasks.Attach(entity);
                    db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
                    db.SaveChanges();
                }
            }
        }
 public static void Delete(OrgChartShape shape)
 {
     if (!UpdateDatabase)
     {
         var target = One(p => p.Id == shape.Id);
         if (target != null)
         {
             All().Remove(target);
         }
     }
     else
     {
         using (var db = new SampleEntities())
         {
             db.OrgChartShapes.Attach(shape);
             db.OrgChartShapes.DeleteObject(shape);
             db.SaveChanges();
         }
     }
 }
        public static void Delete(GanttTaskModel task)
        {
            if (!UpdateDatabase)
            {
                var target = One(p => p.ID == task.ID);
                if (target != null)
                {
                    All().Remove(target);
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = task.ToEntity();
                    db.GanttTasks.Attach(entity);

                    db.GanttTasks.DeleteObject(entity);

                    db.SaveChanges();
                }
            }
        }
        public static void Update(OrgChartShape shape)
        {
            if (!UpdateDatabase)
            {
                var target = One(e => e.Id == shape.Id);

                if (target != null)
                {
                    target.JobTitle = shape.JobTitle;
                    target.Color = shape.Color;
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    db.OrgChartShapes.Attach(shape);
                    db.ObjectStateManager.ChangeObjectState(shape, EntityState.Modified);
                    db.SaveChanges();
                }
            }
        }
        public static void Delete(EmployeeDirectoryModel employee)
        {
            if (!UpdateDatabase)
            {
                var target = One(p => p.EmployeeId == employee.EmployeeId);
                if (target != null)
                {
                    All().Remove(target);

                    var employees = All().Where(m => m.ReportsTo == employee.EmployeeId).ToList();

                    foreach (var subordinate in employees)
                    {
                        Delete(subordinate);
                    }
                }
            }
            else
            {
                using (var db = new SampleEntities())
                {
                    var entity = employee.ToEntity();
                    db.EmployeeDirectory.Attach(entity);

                    var employees = db.EmployeeDirectory.Where(t => t.ReportsTo == employee.EmployeeId);

                    foreach (var subordinate in employees)
                    {
                        Delete(new EmployeeDirectoryModel { EmployeeId = subordinate.EmployeeID });
                    }

                    db.EmployeeDirectory.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
        }