//used #endregion //----------------------Add or update- --------------------------// #region Add or Update functions public Academic.DbEntities.Batches.Batch AddOrUpdateBatch(DbEntities.Batches.Batch batch , List <ProgramBatch> progBatchList) { try { using (var scope = new TransactionScope()) { var ent = Context.Batch.Find(batch.Id); if (ent == null) { ent = Context.Batch.Add(batch); Context.SaveChanges(); foreach (var pb in progBatchList) { pb.BatchId = ent.Id; Context.ProgramBatch.Add(pb); Context.SaveChanges(); } } else { ent.Name = batch.Name; ent.Description = batch.Description; //ent.ClassCommenceDate = batch.ClassCommenceDate; //ent.Void = batch.Void 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(); } } } scope.Complete(); return(ent); } } catch { return(null); } }
//-------------------------------------- GET -------------------------// #region Get functions --> i.e. all the functions that return single object public DbEntities.Batches.Batch GetBatch(int batchId) { var batch = Context.Batch.Include(i => i.AcademicYear).FirstOrDefault(x => x.Id == batchId); if (batch == null) { batch = new DbEntities.Batches.Batch() { Id = 0 , Name = "" }; } return(batch); }
//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); } }