示例#1
0
        static void ReadDB()
        {
            using (BloggingDbContext db = new BloggingDbContext())
            {
                List <SubjectStudent> subjectStudents = new List <SubjectStudent>(db.SubjectStudents);

                foreach (var subjectStudent in subjectStudents)
                {
                    var studentName = (from st in db.Students
                                       where st.StudentId == subjectStudent.StudentId
                                       select st.StudentName).First();


                    var subjectName = (from sub in db.Subjects
                                       where sub.SubjectId == subjectStudent.SubjectId
                                       select sub.SubjectName).First();


                    Console.Write(studentName + " student's grade in subject " + subjectName + " is  ");

                    subjectStudent.Grade = Convert.ToDouble(Console.ReadLine());
                }

                db.SaveChanges();
            }
        }
示例#2
0
        static void CreateDB()
        {
            List <Subject> subjects = new List <Subject>()
            {
                new Subject()
                {
                    SubjectName = "Operating Systems"
                },
                new Subject()
                {
                    SubjectName = "Security"
                },
                new Subject()
                {
                    SubjectName = "Databases"
                },
                new Subject()
                {
                    SubjectName = ".NET"
                },
                new Subject()
                {
                    SubjectName = "Web"
                },
            };

            Random randomGenerator = new Random();

            List <Student> students = new List <Student>();

            for (int i = 0; i < 50; i++)
            {
                Student student = new Student()
                {
                    StudentName = studentNames[randomGenerator.Next(studentNamesCount)]
                };

                List <SubjectStudent> subjectStudent = new List <SubjectStudent>();

                for (int j = 0; j < randomGenerator.Next(2, 4); j++)
                {
                    int subjectId = randomGenerator.Next(5);
                    for (int k = 0; k < j; k++)
                    {
                        while (subjects[subjectId].SubjectName == subjectStudent[k].Subject.SubjectName)
                        {
                            subjectId = randomGenerator.Next(5);
                            k         = 0;
                        }
                    }
                    subjectStudent.Add(new SubjectStudent()
                    {
                        Student = student, Subject = subjects[subjectId]
                    });
                }

                student.Subjects = subjectStudent;
                students.Add(student);
            }

            using (BloggingDbContext db = new BloggingDbContext())
            {
                foreach (var student in students)
                {
                    db.Students.Add(student);
                }

                db.SaveChanges();
            }
        }