コード例 #1
0
ファイル: One2ManyClass.cs プロジェクト: zealoussnow/OneCode
        // 插入新的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
        // Insert new course to existing 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);
                }
            }
        }
コード例 #3
0
ファイル: One2ManyClass.cs プロジェクト: zealoussnow/OneCode
        // 插入新的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);
                }
            }
        }
コード例 #4
0
        // Insert new department with new 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);
                }
            }
        }
コード例 #5
0
        // Update one existing 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);
                }
            }
        }
コード例 #6
0
        // Update one existing 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);
                }
            }
        }
コード例 #7
0
        // Get all the departments with courses
        public static void Query()
        {
            using (EFO2MEntities context = new EFO2MEntities())
            {
                var query = from p in context.Department.Include("Course")
                            select p;

                Console.WriteLine("Deparments with their courses:");
                foreach (Department d in query)
                {
                    Console.WriteLine("{0} {1}", d.DepartmentID, d.Name);

                    foreach (Course c in d.Course)
                    {
                        Console.WriteLine("   {0}", c.Title);
                    }
                }

                Console.WriteLine();
            }
        }
コード例 #8
0
ファイル: One2ManyClass.cs プロジェクト: zealoussnow/OneCode
        // 更新一个现存的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);
                }
            }
        }
コード例 #9
0
ファイル: One2ManyClass.cs プロジェクト: zealoussnow/OneCode
        // 更新一个现存的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);
                }
            }
        }
コード例 #10
0
ファイル: One2ManyClass.cs プロジェクト: zealoussnow/OneCode
        // 获取带有courses的所有departments
        public static void Query()
        {
            using (EFO2MEntities context = new EFO2MEntities())
            {
                var query = from p in context.Department.Include("Course")
                            select p;

                Console.WriteLine("Deparments with their courses:");
                foreach (Department d in query)
                {
                    Console.WriteLine("{0} {1}", d.DepartmentID, d.Name);

                    foreach (Course c in d.Course)
                    {
                        Console.WriteLine("   {0}", c.Title);
                    }
                }

                Console.WriteLine();
            }
        }