static void AddNewDepartmentAndCourses(int departmentID, int courseID)
        {
            using (var service = new Service1Client())
            {
                Department newDepartment = new Department()
                {
                    DepartmentID = departmentID,
                    Budget = 13000.000m,
                    Name = "New Department",
                    StartDate = DateTime.Now
                };

                OnlineCourse newCourse = new OnlineCourse()
                {
                    CourseID = courseID,
                    DepartmentID = departmentID,
                    URL = "http://www.fineartschool.net/Trigonometry",
                    Title = "New Onsite Course",
                    Credits = 4
                };

                // Add the course to the department.
                newDepartment.Courses.Add(newCourse);

                // The newly create objects are marked as added, the service will insert these into the store.
                service.UpdateDepartment(newDepartment);

                // Let’s make few more changes to the saved object.
                // Since the previous changes have now been persisted, call AcceptChanges to
                // reset the ChangeTracker on the objects and mark the state as ObjectState.Unchanged.
                // Note, AcceptChanges sets the tracking on, so you do not need to call StartTracking
                // explicitly.
                newDepartment.AcceptChanges();
                newCourse.AcceptChanges();

                // Because the change tracking is enabled
                // the following change will set newCourse.ChangeTracker.State to ObjectState.Modified.
                newCourse.Credits = 6;
                service.UpdateDepartment(newDepartment);

            }
        }
        /// <summary>
        /// Updates department and its related courses. 
        /// </summary>
        public void UpdateDepartment(Department updated)
        {
            using (SchoolEntities context =
                new SchoolEntities())
            {
                try
                {
                    // Perform validation on the updated order before applying the changes.

                    // The ApplyChanges method examines the change tracking information
                    // contained in the graph of self-tracking entities to infer the set of operations
                    // that need to be performed to reflect the changes in the database.
                    context.Departments.ApplyChanges(updated);
                    context.SaveChanges();

                }
                catch (UpdateException ex)
                {
                    // To avoid propagating exception messages that contain sensitive data to the client tier,
                    // calls to ApplyChanges and SaveChanges should be wrapped in exception handling code.
                    throw new InvalidOperationException("Failed to update the department. Try your request again.");
                }
            }
        }
        private void FixupDepartment(Department previousValue)
        {
            if (IsDeserializing)
            {
                return;
            }

            if (previousValue != null && previousValue.Courses.Contains(this))
            {
                previousValue.Courses.Remove(this);
            }

            if (Department != null)
            {
                if (!Department.Courses.Contains(this))
                {
                    Department.Courses.Add(this);
                }

                DepartmentID = Department.DepartmentID;
            }
            if (ChangeTracker.ChangeTrackingEnabled)
            {
                if (ChangeTracker.OriginalValues.ContainsKey("Department")
                    && (ChangeTracker.OriginalValues["Department"] == Department))
                {
                    ChangeTracker.OriginalValues.Remove("Department");
                }
                else
                {
                    ChangeTracker.RecordOriginalValue("Department", previousValue);
                }
                if (Department != null && !Department.ChangeTracker.ChangeTrackingEnabled)
                {
                    Department.StartTracking();
                }
            }
        }