示例#1
0
            public DbSetup(WebApplicationFactory <Startup> factory)
            {
                // This fetches the same single lifetime instantiation used by Controller classes
                _dbContext = factory.Services.GetRequiredService <RosterDbContext>();

                // Seed in-memory database with some data needed for tests
                var school = new School
                {
                    Id    = 1,
                    Name  = "School of Hard Knocks",
                    City  = "Life",
                    State = "Madness"
                };

                _dbContext.School.Add(school);
                var teacher = new Teacher
                {
                    Id     = 1,
                    Name   = "Mrs. Stricter",
                    School = school
                };

                _dbContext.Teacher.Add(teacher);
                var @class = new Class
                {
                    Id      = 1,
                    Name    = "Fifth Grade Class",
                    Teacher = teacher
                };

                _dbContext.Class.Add(@class);
                var student1 = new Student
                {
                    Id    = 1,
                    Name  = "Jim Bob",
                    Class = @class
                };

                _dbContext.Student.Add(student1);
                var student2 = new Student
                {
                    Id    = 2,
                    Name  = "Jane Doe",
                    Class = @class
                };

                _dbContext.Student.Add(student2);
                _dbContext.SaveChanges();
            }
示例#2
0
            public void Dispose()
            {
                var students = _dbContext.Student.ToArray();

                _dbContext.Student.RemoveRange(students);
                var classes = _dbContext.Class.ToArray();

                _dbContext.Class.RemoveRange(classes);
                var teachers = _dbContext.Teacher.ToArray();

                _dbContext.Teacher.RemoveRange(teachers);
                var schools = _dbContext.School.ToArray();

                _dbContext.School.RemoveRange(schools);
                _dbContext.SaveChanges();
            }