public void Seed() { var school = _context.School.First(x => x.Code == SchoolSeeder.NEC_CODE); var computerProgram = _context.Program.First(x => x.Name == ProgramSeeder.COMPUTER_PROGRAM_NAME); var academicYears = new DbEntities.AcademicYear[] { new DbEntities.AcademicYear { StartDate = new DateTime(DateTime.Now.Year, 1, 1), EndDate = new DateTime(DateTime.Now.Year, 12, 31), IsActive = true, Name = DateTime.Now.Year.ToString(), SchoolId = school.Id, Sessions = new List<DbEntities.Session> { new DbEntities.Session{Name = "Fall", StartDate = new DateTime(DateTime.Now.Year, 1,1), Position = 1, EndDate = new DateTime(DateTime.Now.Year, 6, 15)}, new DbEntities.Session{Name = "Spring", StartDate = new DateTime(DateTime.Now.Year, 6,16), Position = 1, EndDate = new DateTime(DateTime.Now.Year, 12, 31)}, }, Batches = new List<Academic.DbEntities.Batches.Batch> { new DbEntities.Batches.Batch { Name = DateTime.Now.Year.ToString().Substring(1), CreatedDate = DateTime.Now, Description = "", SchoolId = school.Id, ProgramBatches = new List<DbEntities.Batches.ProgramBatch> { new DbEntities.Batches.ProgramBatch{ProgramId = computerProgram.Id, } } }, }, Position = 1, } }; _context.AcademicYear.AddOrUpdate( p => p.Name, academicYears ); _context.SaveChanges(); }
//Used v-2 public DbEntities.Batches.Batch AddOrUpdateAcademicYearAndBatch(int schoolId, DbEntities.AcademicYear academicY, List <Session> sessions, DbEntities.Batches.Batch batch, List <DbEntities.Batches.ProgramBatch> progBatchList) { var acaEntity = Context.AcademicYear.Find(academicY.Id); var batchEnt = Context.Batch.Find(batch.Id); try { using (TransactionScope scope = new TransactionScope()) { if (acaEntity == null) { //add #region Academic year //var max = academicY.StartDate.Year+academicY.StartDate.Month+academicY.StartDate.Day; //try //{ // max = Context.AcademicYear.Where(x => x.SchoolId == schoolId).Max(m => m.Position); //} //catch { } //academicY.Position = max ; acaEntity = Context.AcademicYear.Add(academicY); Context.SaveChanges(); foreach (var session in sessions) { session.AcademicYearId = acaEntity.Id; Context.Session.Add(session); Context.SaveChanges(); } #endregion #region Batch batch.AcademicYearId = acaEntity.Id; batchEnt = Context.Batch.Add(batch); Context.SaveChanges(); foreach (var pb in progBatchList) { pb.BatchId = batchEnt.Id; Context.ProgramBatch.Add(pb); Context.SaveChanges(); } #endregion //saveSuccess = true; } else { //update #region Academic year acaEntity.IsActive = academicY.IsActive; acaEntity.Name = academicY.Name; acaEntity.EndDate = academicY.EndDate; acaEntity.SchoolId = academicY.SchoolId; acaEntity.StartDate = academicY.StartDate; acaEntity.Position = academicY.Position; Context.SaveChanges(); foreach (var session in sessions) { var foundSession = Context.Session.Find(session.Id); if (foundSession == null) { Context.Session.Add(session); } else { foundSession.Name = session.Name; foundSession.StartDate = session.StartDate; foundSession.EndDate = session.EndDate; } Context.SaveChanges(); } #endregion #region Batch if (batchEnt != null) { batchEnt.Name = batch.Name; batchEnt.Description = batch.Description; Context.SaveChanges(); foreach (var pb in progBatchList) { var found = Context.ProgramBatch.Find(pb.Id); if (found == null) { Context.ProgramBatch.Add(pb); Context.SaveChanges(); } else { found.Void = pb.Void; Context.SaveChanges(); } } batchEnt.AcademicYear = acaEntity; } #endregion //saveSuccess = true; } //var prev = Context.AcademicYear.Where(x => x.SchoolId == acaEntity.SchoolId && x.Id != acaEntity.Id); //foreach (var academicYear in prev) //{ // academicYear.IsActive = false; //} //Context.SaveChanges(); scope.Complete(); return(batchEnt); } } catch (Exception) { return(null); } }
//used v-2 /// <summary> /// Returns list of 3 academic years [0]-previous, [1]-current, [2]-next /// </summary> /// <param name="schoolId"></param> /// <param name="academicYearId"></param> /// <returns></returns> public List <DbEntities.AcademicYear> GetCurrentPreviousAndNextAcademicYears(int schoolId, int academicYearId = 0) { var aca = Context.AcademicYear .Where(x => x.SchoolId == schoolId && !(x.Void ?? false)) .Include(x => x.Sessions) //.OrderBy(x => x.Position) .OrderBy(x => x.StartDate) .ToList(); DbEntities.AcademicYear current = null; DbEntities.AcademicYear previous = null; DbEntities.AcademicYear next = null; if (academicYearId > 0) { current = aca.FirstOrDefault(x => x.Id == academicYearId); if (current != null) { var curIndex = aca.FindIndex(x => x.Id == current.Id); if (curIndex >= 0) { try { previous = aca[curIndex - 1]; } catch { } try { next = aca[curIndex + 1]; } catch { } } } } else { var date = DateTime.Now; current = aca.FirstOrDefault(x => x.IsActive || (x.StartDate <= date && x.EndDate >= date) || x.Sessions.Any(y => y.IsActive || (x.StartDate <= date && x.EndDate >= date))); current = current ?? aca.LastOrDefault(); if (current != null) { var curIndex = aca.FindIndex(x => x.Id == current.Id); try { previous = aca[curIndex - 1]; } catch { } try { next = aca[curIndex + 1]; } catch { } } } var list = new List <DbEntities.AcademicYear>() { previous, current, next }; return(list); }