Пример #1
0
        public static void AddStudent(string Name)
        {
            Students student = new Students();

            student.Name = Name;

            //add a student
            using (var newStudent = new KeithEntityModelContainer()) {
                newStudent.Students.Add(student);
                newStudent.SaveChanges();
            }
        }
Пример #2
0
        public static List <Students> GetStudentList()
        {
            List <Students>           ListOfStudents = new List <Students>();
            KeithEntityModelContainer K = new KeithEntityModelContainer();

            foreach (EntityFramework.Students item in K.Students.Select(row => row))
            {
                ListOfStudents.Add(item);
            }

            return(ListOfStudents);
        }
Пример #3
0
        public static void DeleteStudent(string Name)
        {
            Students studentToDelete;

            //1. Get student from DB
            using (var ctx = new KeithEntityModelContainer()) {
                studentToDelete = ctx.Students.Where(s => s.Name == Name).FirstOrDefault <Students>();
            }

            //Create new context for disconnected scenario
            using (var newContext = new KeithEntityModelContainer()) {
                newContext.Entry(studentToDelete).State = System.Data.Entity.EntityState.Deleted;
                newContext.SaveChanges();
            }
        }
Пример #4
0
        public static void UpdateStudent(string OldName, string NewName)
        {
            Students student;

            //1.Get student from DB
            using (var entityModel = new KeithEntityModelContainer()) {
                student = entityModel.Students.Where(s => s.Name == OldName).FirstOrDefault <Students>();
            }

            //2.change student name
            if (student != null)
            {
                student.Name = NewName;
            }

            //3.save entity
            using (var entityModel = new KeithEntityModelContainer()) {
                //a. Mark as modified
                entityModel.Entry(student).State = System.Data.Entity.EntityState.Modified;
                //b. save changes
                entityModel.SaveChanges();
            }
        }