Example #1
0
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentsSystemContext, Configuration>());
            
            using (StudentsSystemContext dbContext = new StudentsSystemContext())
            {
                var firstHomework = new Homework()
                {
                    Content = "Simple database1",
                    TimeSent = DateTime.Now,
                };

                var secondHomework = new Homework()
                {
                    Content = "Simple database2",
                    TimeSent = DateTime.Now,
                };

                var student = new Student()
                {
                    FirstName = "Nikolay",
                    LastName = "Kostadinov",
                    Number = "965552",
                };

                var course = new Course()
                {
                    Name = "DataBase",
                    Description = "Entity Framework Code First",
                    Materials = "Lections, homework"
                };

                student.Homeworks.Add(firstHomework);
                student.Homeworks.Add(secondHomework);
                student.Courses.Add(course);
                course.Homeworks.Add(secondHomework);
                course.Students.Add(student);

                dbContext.Students.Add(student);
                dbContext.Courses.Add(course);
                dbContext.Homeworks.Add(firstHomework);
                dbContext.Homeworks.Add(secondHomework);

                dbContext.SaveChanges();
            }
        }
        public IHttpActionResult Create(HomeworkModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var newItem = new Homework()
            {
                Content = item.Content,
            };

            this.data.Homeworks.Add(newItem);
            this.data.Homeworks.SaveChanges();

            item.ID = newItem.ID;

            return Ok(item);
        }
        public void Seed(int count)
        {
            Console.Write("Importing homeworks:");

            var db = new StudentsSystemDbContext();
            var allStudentIds = db
                .Students
                .Select(s => s.Id)
                .ToList();
            for (int i = 0; i < count; i++)
            {
                var studentId = RandomGenerator.GetRandomNumber(0, allStudentIds.Count - 1);
                var homework = new Homework
                {
                    Content = RandomGenerator.GetRandomString(50, 150),
                    TimeSent = RandomGenerator.GetRandomDate(before: DateTime.Now),
                    StudentId = allStudentIds[studentId]
                };

                db.Homeworks.Add(homework);

                if (i % 10 == 0)
                {
                    Console.Write(".");
                }

                if (i % 100 == 0)
                {
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentsSystemDbContext();
                }
            }

            db.SaveChanges();
            Console.WriteLine();
        }