Example #1
0
        // 插入新的course到现存的department
        public static void InsertCourse()
        {
            using (EFO2MEntities context = new EFO2MEntities())
            {
                Course course = new Course()
                {
                    CourseID = 2203,
                    Title = "Object Oriented Programming"
                };

                course.Department = (
                    from p in context.Department
                    where p.DepartmentID == 7
                    select p).First();

                context.AddToCourse(course);

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

                    context.SaveChanges();

                    Query();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Example #2
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);
                }
            }
        }
Example #3
0
        // 更新一个现存的course
        public static void UpdateCourse()
        {
            using (EFO2MEntities context = new EFO2MEntities())
            {
                Course course = new Course();

                course.CourseID = 2203;

                context.AttachTo("Course", course);

                course.Title = "OOP";

                try
                {
                    Console.WriteLine("Modifying Course 2203's Title to {0}",
                        course.Title);

                    context.SaveChanges();

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