コード例 #1
0
ファイル: ExamAceSeeder.cs プロジェクト: Balling1991/ExamAce
        public async Task Seed()
        {
            _ctx.Database.EnsureCreated();

            // Ny karakter
            var newGradeViewModel = new GradeViewModel()
            {
                Name = GradeRange.MinusThree
            };

            // Mapping fra GradeViewModel til Grade
            var newGrade = _mapper.Map <GradeViewModel, Grade>(newGradeViewModel);

            // Ny bruger
            var newUserViewModel = new UserViewModel()
            {
                UserName  = "******",
                Email     = "*****@*****.**",
                FirstName = "H**o",
                LastName  = "Kaj",
            };

            // Mapping fra UserViewModel til User
            var newUser = _mapper.Map <UserViewModel, User>(newUserViewModel);

            // Password til ny bruger
            var newPassword = "******";
            //var newPassword = "******";

            var testStudentUser = await _userManager.FindByEmailAsync("*****@*****.**");

            if (testStudentUser == null)
            {
                // Oprettelse af ny bruger
                var createNewUser = await _userManager.CreateAsync(newUser, newPassword);

                if (createNewUser.Succeeded)
                {
                    // Tilføj bruger til rolle
                    await _userManager.AddToRoleAsync(newUser, Roles.Student.ToString());
                }
            }

            // Ny klasse
            var newClassViewModel = new ClassViewModel()
            {
                Name     = "3b",
                Year     = 2018,
                Students = new Collection <Student>()
            };

            // Mapping fra ClassViewModel til Class
            var newClass = _mapper.Map <ClassViewModel, Class>(newClassViewModel);

            // Ny student
            var newStudentViewModel = new StudentViewModel()
            {
                Grades = new Collection <Grade>(),
                Class  = newClass,
                User   = newUser
            };

            // Mapping fra StudentViewModel til Student
            var newStudent = _mapper.Map <StudentViewModel, Student>(newStudentViewModel);

            // Tilføj karakter til student
            newStudent.Grades.Add(newGrade);

            // Tilføj student til klasse
            newClass.Students.Add(newStudent);

            if (!_ctx.Grades.Any())
            {
                _ctx.Grades.Add(newGrade);
            }
            if (!_ctx.Classes.Any())
            {
                _ctx.Classes.Add(newClass);
            }
            if (!_ctx.Students.Any())
            {
                _ctx.Students.Add(newStudent);
            }
            _ctx.SaveChanges();
        }
コード例 #2
0
 public bool SaveAll()
 {
     return(_ctx.SaveChanges() > 0);
 }