Пример #1
0
        static void Main(string[] args)
        {
            Test t = new Test();

            // 新增学校.
            School school1 = new School()
            { 
                SchoolName = "学校1"
            };
            School school2 = new School()
            {
                SchoolName = "学校2"
            };

            t.SaveSchoolData(school1);
            t.SaveSchoolData(school2);


            // 新增老师.
            Teacher t1 = new Teacher()
            {
                TeacherName = "张老师",
                InSchool = school1
            };

            Teacher t2 = new Teacher()
            {
                TeacherName = "李老师",
                InSchool = school1
            };

            Teacher t3 = new Teacher()
            {
                TeacherName = "王老师",
                InSchool = school2
            };

            Teacher t4 = new Teacher()
            {
                TeacherName = "赵老师",
                InSchool = school2
            };

            t.SaveTeacherData(t1);
            t.SaveTeacherData(t2);
            t.SaveTeacherData(t3);
            t.SaveTeacherData(t4);

            
            // 查询 学校中的老师.
            foreach (School sc in t.SchoolDataSource)
            {
                foreach (Teacher te in sc.SchoolTeachers)
                {
                    Console.WriteLine("{0}:{1}", sc.SchoolName, te.TeacherName);
                }
            }

            Console.ReadLine();
        }
Пример #2
0
 /// <summary>
 /// 保存.
 /// </summary>
 /// <param name="data"></param>
 public void SaveTeacherData(Teacher data)
 {
     if (data.TeacherID == 0)
     {
         context.TeacherDbSet.Add(data);
     }
     else
     {
         context.Entry(data).State = EntityState.Modified;
     }
     context.SaveChanges();
 }