static void SetCourseLevels() { Console.WriteLine("Fetching courses..."); var courses = _context.Courses.ToList(); foreach (var item in courses) { if (item.Years == 0) { Console.WriteLine($"Updating {item.Name}..."); item.Years = 4; _context.Entry(item).State = EntityState.Modified; } } Console.WriteLine("Updating DONE!"); Console.WriteLine("Saving changes to database"); _context.SaveChanges(); Console.WriteLine("Operation completed successfully!"); }
public async Task <ActionResult> Profile() { try { int type = int.Parse(Request.Form["Type"]); var user = await _userManager.GetUserAsync(User); var profile = new Profile { FullNames = Request.Form["FullNames"], NationalID = int.Parse(Request.Form["ID"]) }; if (Request.Form.Files.Count > 0) { IFile file = new FormFile(Request.Form.Files[0]); profile.PhotoUrl = await _uploader.Upload(file); } if (type == 1) { //lec Lecturer lecturer = new Lecturer(Guid.Parse(user.Id)) { Profile = profile }; _lepadContext.Lecturers.Add(lecturer); _lepadContext.SaveChanges(); user.AccountId = lecturer.Id; user.AccountType = AccountType.Lecturer; } else if (type == 2) { //student Student student = new Student(Guid.Parse(user.Id)) { YearOfStudy = int.Parse(Request.Form["YrOfStudy"]), AcademicYear = Request.Form["AcademicYr"], RegNo = Request.Form["RegNo"], Profile = profile, }; _lepadContext.Students.Add(student); _lepadContext.SaveChanges(); user.AccountId = student.Id; user.AccountType = AccountType.Student; } else if (type == 0) { // admin Admin admin = new Admin(Guid.Parse(user.Id)) { Profile = profile }; _lepadContext.Administrators.Add(admin); _lepadContext.SaveChanges(); user.AccountId = admin.Id; user.AccountType = AccountType.Administrator; } // add claims we need in the app (userId, accountType) var claims = new List <Claim> { new Claim("UserId", user.Id), new Claim("ProfileId", profile.Id.ToString()) }; if (!string.IsNullOrWhiteSpace(profile.FullNames)) { claims.Add(new Claim("FullNames", profile.FullNames)); } if (!string.IsNullOrWhiteSpace(profile.PhotoUrl)) { claims.Add(new Claim("PhotoUrl", profile.PhotoUrl)); } await _userManager.AddClaimsAsync(user, claims); //update user identity account var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return(RedirectPermanent("/")); } else { return(Content(result.Errors.First().Description)); } } catch (Exception ex) { return(Content(ex.Message)); } }
public void Commit() { _context.SaveChanges(); }