예제 #1
0
        // 插入新的department包括新的course
        public static void InsertDepartmentWithCourse()
        {
            using (EFO2MEntities context = new EFO2MEntities())
            {
                Department department = new Department()
                {
                    DepartmentID = 4,
                    Name = "Software Engineering"
                };

                Course course = new Course()
                {
                    CourseID = 2202,
                    Title = "ADO.NET"
                };

                department.Course.Add(course);

                context.AddToDepartment(department);

                try
                {
                    Console.WriteLine("Inserting department {0} with course "
                        + "{1}", department.Name, course.Title);

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
예제 #2
0
        // 更新一个现存的department
        public static void UpdateDepartment()
        {
            using (EFO2MEntities context = new EFO2MEntities())
            {
                Department department = new Department();

                department.DepartmentID = 1;

                context.AttachTo("Department", department);

                department.Name = "Computer Engineering";

                department.Course.Add(new Course()
                {
                    CourseID = 2204,
                    Title = "Arithmetic"
                });

                try
                {
                    Console.WriteLine("Modifying Department 1's Title to {0}"
                        + ", and insert a new Course 2204 into the " +
                        "Department 1", department.Name);

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }