Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            HasData();

            IEnumerable <string> data = DbClass.ReadCSV(@"data.csv");


            int countCurrent = 0, countGood = 0, countBad = 0;

            using (BloggingDbContext db = new BloggingDbContext())
            {
                foreach (var item in data)
                {
                    countCurrent++;

                    if ((item == "StudentId,SubjectId,Point") || (item == ""))
                    {
                        continue;
                    }

                    int[] splitted = DbClass.SplitString(item);

                    if (splitted == null)
                    {
                        return;
                    }

                    int studentId, subjectId, point;
                    studentId = splitted[0];
                    subjectId = splitted[1];
                    point     = splitted[2];


                    if (!(point >= 0 && point <= 100))
                    {
                        countBad++;
                        continue;
                    }

                    SubjectStudent subjectStudent = new SubjectStudent();
                    subjectStudent = (from st in db.SubjectStudents
                                      where st.StudentId == studentId
                                      where st.SubjectId == subjectId
                                      select st).FirstOrDefault();

                    if (subjectStudent == null)
                    {
                        countBad++;
                        continue;
                    }

                    subjectStudent.Point = point;
                    db.SaveChanges();
                    countGood++;
                }
            }

            Console.WriteLine("\nImported " + countGood + " items.\n" +
                              "Failed to import " + countBad + " items.");
        }
Ejemplo n.º 2
0
        static void HasData()
        {
            List <Student> students = new List <Student>(DbClass.CreateStudents());

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

                db.SaveChanges();
            }
        }