コード例 #1
0
        public int AddStudent(Student student)
        {
            using (var db = new UnivercityContext())
            {
                try
                {
                    Student newStudent = new Student()
                    {
                        Name = student.Name,
                        Surname = student.Surname,
                        Age = student.Age,
                        Sex = student.Sex,
                        SectionId = student.SectionId,
                        CurrentClass = student.CurrentClass,
                        EnrollmentDate = student.EnrollmentDate
                    };
                    db.Students.Add(newStudent);
                    db.SaveChanges();

                    // while db.saveChanges() newStudent.ID is initialized,
                    // so we can get it after that.
                    return newStudent.Id;
                }
                catch (Exception)
                {
                    return -1;
                }
            }
        }
コード例 #2
0
 public bool DeleteStudent(int StudentId)
 {
     using (var db = new UnivercityContext())
     {
         var student = new Student { Id = StudentId };
         db.Students.Attach(student);
         db.Students.Remove(student);
         db.SaveChanges();
     }
     return true;
 }
コード例 #3
0
        public bool ModifyStudent(Student std)
        {
            using (var db = new UnivercityContext())
            {
                var original = db.Students.Find(std.Id);

                if (original != null)
                {
                    original.Name = std.Name;
                    original.Surname = std.Surname;
                    original.Age = std.Age;
                    original.CurrentClass = std.CurrentClass;
                    original.EnrollmentDate = std.EnrollmentDate;
                    original.Sex = std.Sex;
                    db.SaveChanges();
                }

                // advanced method, but the problems is that i've not managed sectionId
                // so that it becomes 0 within this method  // todo later --> means UI modif :(

                //if (original != null)
                //{
                //    db.Entry(original).CurrentValues.SetValues(std);
                //    db.SaveChanges();
                //    return true;
                //}

                return false;
            }
        }