Ejemplo n.º 1
0
        private static void AddStudent(string firstName, string lastName, DateTime enrollmentDate, UniversityModelFirstDbContext customDbContext = null)
        {
            UniversityModelFirstDbContext db = null;

            try
            {
                db = customDbContext ?? new UniversityModelFirstDbContext();
                var student = new Student
                {
                    LastName       = lastName,
                    FirstName      = firstName,
                    EnrollmentDate = enrollmentDate
                };
                db.Students.Add(student);
                db.SaveChanges();
                Console.WriteLine("Added...");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (customDbContext == null)
                {
                    db?.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        private static IEnumerable <string> ListStudents(UniversityModelFirstDbContext customDbContext = null)
        {
            var students = new LinkedList <string>();
            UniversityModelFirstDbContext db = null;

            try
            {
                db = customDbContext ?? new UniversityModelFirstDbContext();
                var query = from b in db.Students orderby b.ID select b;
                foreach (var item in query)
                {
                    students.AddLast($"{item.ID} - {item.FirstName} {item.LastName}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (customDbContext == null)
                {
                    db?.Dispose();
                }
            }
            return(students);
        }
Ejemplo n.º 3
0
        private static void ModifyStudent(string filterFirstName, string newFirstName = null, string newLastName = null, UniversityModelFirstDbContext customDbContext = null)
        {
            UniversityModelFirstDbContext db = null;

            try
            {
                db = customDbContext ?? new UniversityModelFirstDbContext();
                var student = (from d in db.Students where d.FirstName == filterFirstName select d).Single();
                student.LastName  = string.IsNullOrEmpty(newLastName) ? student.LastName : newLastName;
                student.FirstName = string.IsNullOrEmpty(newFirstName) ? student.FirstName : newFirstName;
                db.SaveChanges();
                Console.WriteLine("Modified...");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (customDbContext == null)
                {
                    db?.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        private static void DeleteStudent(string filterFirstName, UniversityModelFirstDbContext customDbContext = null)
        {
            UniversityModelFirstDbContext db = null;

            try
            {
                db = customDbContext ?? new UniversityModelFirstDbContext();
                var student = (from d in db.Students where d.FirstName == filterFirstName select d).Single();
                db.Students.Remove(student);
                db.SaveChanges();
                Console.WriteLine("Deleted...");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (customDbContext == null)
                {
                    db?.Dispose();
                }
            }
        }