private void _F_AddStudent() { var sInstance = ServiceLocator.Current.GetInstance <StudentModifyViewModel>(); sInstance.studentInfo = new StudentInfo(); sInstance.classInfo = Classes.FirstOrDefault(); if (sInstance.classInfo == null) { return; } sInstance.Title = "添加学生信息"; StudentInfoWindow studentInfoWindow = new StudentInfoWindow(); studentInfoWindow.ShowDialog(); if (studentInfoWindow.DialogResult == true) { var target = sInstance.studentInfo; target.Age = DateTime.Now.Year - target.DateOfBirth.Year; _StudentInfoDal.Insert(target); _StudentInfoDal.Save(); _RelationShipDal.Insert(new Student_Class_Relation() { StudentId = sInstance.studentInfo.ID, ClassId = sInstance.classInfo.ID }); _RelationShipDal.Save(); StudentList.Add(new StudentInfoViewModel(target)); } }
private void _F_UpdateStudent(StudentInfoViewModel s) { var sInstance = ServiceLocator.Current.GetInstance <StudentModifyViewModel>(); sInstance.studentInfo = s.StudentInfo; sInstance.Title = "修改学生信息"; sInstance.classInfo = (from citem in _EFDataContext.classes from item in _EFDataContext.relations where item.StudentId == s.ID && item.ClassId == citem.ID select citem).ToList()?.FirstOrDefault(); StudentInfoWindow studentInfoWindow = new StudentInfoWindow(); studentInfoWindow.ShowDialog(); if (studentInfoWindow.DialogResult == true) { _StudentInfoDal.Update(s.StudentInfo); var target = (from item in Relation where item.StudentId == s.ID select item).ToList(); if (target != null && target.Count > 0) { target[0].ClassId = sInstance.classInfo.ID; _RelationShipDal.Update(target[0]); } else { _RelationShipDal.Insert(new Student_Class_Relation() { StudentId = s.ID, ClassId = sInstance.classInfo.ID }); } _StudentInfoDal.Save(); _RelationShipDal.Save(); s.Update(); //RefreshData(); //RaisePropertyChanged("Students"); } }