Exemplo n.º 1
0
        public static void AddStudent()
        {
            using (var context = new EFExampleBasicContext())
            {
                var someClassId = context.Classes
                                  .Where(studyClass => studyClass.Name == "TA-81")
                                  .Select(studyClass => studyClass.Id)
                                  .FirstOrDefault();

                var newStudent = new Student()
                {
                    FirstName = "Anufriy",
                    LastName  = "Korch",
                    Address   = new Address()
                    {
                        City       = "Kiev",
                        FirstLine  = "Troeshina",
                        SecondLine = "Amin'"
                    },
                    StudyClassId = someClassId
                };

                context.Students.Add(newStudent);
                context.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public static void ModifyStudent()
        {
            using (var context = new EFExampleBasicContext())
            {
                var firstStudent = context.Students.FirstOrDefault();

                firstStudent.FirstName = "modifiedName";

                var entry = context.Entry(firstStudent);

                context.SaveChanges();
            }
        }
Exemplo n.º 3
0
        public static void AddStudyClass()
        {
            using (var context = new EFExampleBasicContext()) {
                var newClass = new StudyClass()
                {
                    Name      = "TA-81",
                    StartYear = 2017,
                };

                context.Classes.Add(newClass);
                context.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public static void ShowAllClassesAndStudents()
        {
            using (var context = new EFExampleBasicContext())
            {
                var classes = context.Classes.ToList();

                foreach (var studyClass in classes)
                {
                    var studentsCount = studyClass.Students.Count;
                    Console.WriteLine("Class {0} has {1} students", studyClass.Name, studentsCount);

                    foreach (var student in studyClass.Students)
                    {
                        Console.WriteLine("Student {0} {1}. From {2}, {3}, {4}. Born {5}",
                                          student.FirstName,
                                          student.LastName,
                                          student.Address.City,
                                          student.Address.FirstLine,
                                          student.Address.SecondLine,
                                          student.DateOfBirth ?? "somewhen");
                    }
                }
            }
        }