コード例 #1
0
        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);

            }
        }