public void PromoteStudents(StudiesSemester studiesSemester) { s19677Context db = new s19677Context(); //W bazie mam już procedurę promoteStudents, więc można ją uruchomić w taki sposób: // db.Database.ExecuteSqlRaw("EXEC promoteStudents"); //lub wykonać instrukcję UPDATE ręcznie: var study = db.Studies.FirstOrDefault(s => s.Name == studiesSemester.Studies); var enrol = db.Enrollment.FirstOrDefault(e => e.IdStudy == study.IdStudy && e.Semester == studiesSemester.Semester); if (db.Enrollment.Contains(enrol)) { Boolean existsEnrol2 = db.Enrollment.Any(e => e.IdStudy == study.IdStudy && e.Semester == studiesSemester.Semester + 1); var en0 = db.Enrollment.FirstOrDefault(e => e.IdStudy == study.IdStudy && e.Semester == studiesSemester.Semester + 1); int id; if (!existsEnrol2) { var en = new EntityModels.Enrollment { IdEnrollment = 99, Semester = studiesSemester.Semester + 1, IdStudy = study.IdStudy, StartDate = DateTime.Now }; id = en.IdEnrollment; db.Add(en); db.SaveChanges(); } else { id = en0.IdEnrollment; } foreach (var s in db.Student.Where(s => s.IdEnrollment == enrol.IdEnrollment)) { s.IdEnrollment = id; } db.SaveChanges(); } }
public Enrollment PromoteStudents([FromBody] StudiesSemester StudiesSemester) { using (SqlConnection connection = new SqlConnection(ConnectionString)) using (SqlCommand command = new SqlCommand("promoteStudents", connection)) { connection.Open(); command.Parameters.AddWithValue("@studies", StudiesSemester.Studies); command.Parameters.AddWithValue("@semester", StudiesSemester.Semester); command.CommandType = System.Data.CommandType.StoredProcedure; command.ExecuteNonQuery(); Enrollment enrollment = new Enrollment { Semester = StudiesSemester.Semester + 1, Studies = StudiesSemester.Studies }; return(enrollment); } }
public IActionResult PromoteStudents([FromBody] StudiesSemester StudiesSemester) { _service.PromoteStudents(StudiesSemester); return(Ok("Students promoted")); }