public static void CreateOrUpdateCompetitionDiscipline(Guid disciplinePKey, DisciplineType type, string name, string unit, bool lowIsBetter) { using (HonglornDb db = new HonglornDb()) { CompetitionDiscipline competition = db.CompetitionDiscipline.Find(disciplinePKey); if (competition == null) { // Create db.CompetitionDiscipline.Add(new CompetitionDiscipline { Type = type, Name = name, Unit = unit, LowIsBetter = lowIsBetter }); } else { // Update competition.Type = type; competition.Name = name; competition.Unit = unit; competition.LowIsBetter = lowIsBetter; } db.SaveChanges(); } }
/// <summary> /// Imports data of a single student into the database. /// </summary> /// <remarks></remarks> static void ImportSingleStudent(Student student, string courseName, short year) { //todo: handle exception GetClassName(courseName); //check whether the course name can be mapped to a class name //todo: verify year using (HonglornDb db = new HonglornDb()) { IQueryable <Student> studentQuery = from s in db.Student where s.Forename == student.Forename && s.Surname == student.Surname && s.Sex == student.Sex && s.YearOfBirth == student.YearOfBirth select s; Student existingStudent = studentQuery.SingleOrDefault(); if (existingStudent == null) { Student newStudent = new Student { Forename = student.Forename, Surname = student.Surname, Sex = student.Sex, YearOfBirth = student.YearOfBirth }; newStudent.AddStudentCourseRel(year, courseName); db.Student.Add(newStudent); } else { IEnumerable <StudentCourseRel> courseInformationQuery = from r in existingStudent.StudentCourseRel where r.Year == year select r; StudentCourseRel existingCourseInformation = courseInformationQuery.SingleOrDefault(); if (existingCourseInformation == null) { existingStudent.AddStudentCourseRel(year, courseName); } else { existingCourseInformation.CourseName = courseName; } } db.SaveChanges(); } }
public static void UpdateSingleStudentCompetition(Guid studentPKey, short year, float?sprint, float?jump, float? @throw, float?middleDistance) { using (HonglornDb db = new HonglornDb()) { Student student = db.Student.Find(studentPKey); if (student != null) { Competition existingCompetition = (from c in student.Competition where c.Year == year select c).SingleOrDefault(); if ((sprint ?? jump ?? @throw ?? middleDistance) == null) { // Delete if (existingCompetition != null) { db.Competition.Remove(existingCompetition); } } else { if (existingCompetition == null) { // Create student.Competition.Add(new Competition { Year = year, Sprint = sprint, Jump = jump, Throw = @throw, MiddleDistance = middleDistance }); } else { // Update existingCompetition.Sprint = sprint; existingCompetition.Jump = jump; existingCompetition.Throw = @throw; existingCompetition.MiddleDistance = middleDistance; } } db.SaveChanges(); } else { throw new ArgumentException($"No {nameof(Student)} with such key in database: {studentPKey}"); } } }
public static void DeleteCompetitionDisciplineByPKey(Guid pKey) { try { using (HonglornDb db = new HonglornDb()) { CompetitionDiscipline discipline = new CompetitionDiscipline { PKey = pKey }; db.Entry(discipline).State = EntityState.Deleted; db.SaveChanges(); } } catch (Exception ex) { throw new ArgumentException($"A {nameof(CompetitionDiscipline)} with PKey {pKey} does not exist in the database.", ex); } }