public T Update(T entity) { if (entity == null) { throw new ArgumentNullException("entity"); } _context.Attach(entity).State = EntityState.Modified; return(entity); }
private void RemoveStudentBtn_MouseClick(object sender, MouseEventArgs e) { if (studentsList.SelectedItems.Count == 0) { MessageBox.Show("Vyberte v tabulce řádek studenta.", "Smazat studenta", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (studentsList.SelectedItems.Count > 0) { if (MessageBox.Show("Chystáte se smazat " + studentsList.SelectedItems.Count + " studentů. Pokračovat?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } } using (StudentDbContext db = new StudentDbContext()) { foreach (ListViewItem item in studentsList.SelectedItems) { Student student = null; studentsInListView.Keys.ToList().ForEach(x => { if (studentsInListView[x] == item) { student = x; return; } }); if (student == null) { return; } db.Attach(student); db.Entry(student).Collection(x => x.Presences).Load(); student.Presences.ForEach(x => { db.Presences.Remove(x); }); db.Students.Remove(student); OnStudentRemovedAction.Invoke(student); } db.SaveChanges(); } }
public IActionResult UpdateStudent(ChangeRequest stud) { var student = _context.Student.Where(stu => stu.IndexNumber == stud.IndexNumber); Student changed = new Student { IndexNumber = stud.IndexNumber, FirstName = stud.FirstName, LastName = stud.LastName, Birthdate = stud.Birthdate, Studies = stud.Studies, IdEnrollment = stud.IdEnrollment }; _context.Attach(changed); _context.Entry(changed).Property("FirstName").IsModified = true; _context.Entry(changed).Property("LastName").IsModified = true; _context.Entry(changed).Property("BirthDate").IsModified = true; _context.Entry(changed).Property("Studies").IsModified = true; _context.Entry(changed).Property("IdEnrollment").IsModified = true; _context.SaveChanges(); return(Ok("Zaktualizowano studenta o numerze:" + changed.IndexNumber)); }
private void saveBtn_Click(object sender, EventArgs e) { _dateTime = dateTimePicker1.Value; _dateTime = DateTime.Parse(_dateTime.ToShortDateString()); bool newPresence = GetPresence() == false; if (presences.ContainsKey(absenceTypeBox.Text)) { Presence.Type = presences[absenceTypeBox.Text]; try { using (StudentDbContext db = new StudentDbContext()) { db.Attach(_student); if (newPresence) { _student.Presences.Add(Presence); Presence.Date = _dateTime; Presence.Student = _student; db.Add(Presence); } else { db.Presences.Update(Presence); } db.SaveChanges(); Console.WriteLine(Presence.ToString()); } } catch (Exception exception) { Console.WriteLine(exception); MessageBox.Show("Prezenci nebylo možné uložit.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } Close(); } else { MessageBox.Show("Prezence " + absenceTypeBox.SelectedText + " neexistuje!", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private bool GetPresence() { using (StudentDbContext db = new StudentDbContext()) { db.Attach(_student); db.Entry(_student).Collection(s => s.Presences) .Query().Where(p => (p.Date.Equals(_dateTime))).Load(); if (_student.Presences.Count != 0) { Presence = _student.Presences.Find(x => x.Date.Equals(_dateTime)); } if (Presence == null) { Presence = new Presence(); return(false); } return(true); } }
private void ShowValues() { using (StudentDbContext db = new StudentDbContext()) { foreach (Student student in db.Students) { db.Attach(student); db.Entry(student).Collection(s => s.Presences).Load(); string[] arr = new string[presenceList.Columns.Count]; arr[0] = student.Name; int present = GetPresenceCount(student, PresenceType.Present); int absent = GetPresenceCount(student, PresenceType.Absent); int excused = GetPresenceCount(student, PresenceType.Excused); int totalAbsence = absent + excused; arr[1] = present.ToString(); arr[2] = absent.ToString(); arr[3] = excused.ToString(); arr[4] = totalAbsence.ToString(); presenceList.Items.Add(new ListViewItem(arr)); lines.Add(arr); } } }