private void saveCacheIntoDb() { #region Deleting from db var cacheTeacherIdsHashSet = new HashSet <int>(cache.Rows.OfType <DataRow>() .Select(dr => dr.Field <int>("TeacherId")).ToArray()); var dbTeacherIdsHashSet = new HashSet <int>(context.Teachers.Select(t => t.Id).ToArray()); dbTeacherIdsHashSet.ExceptWith(cacheTeacherIdsHashSet); foreach (int id in dbTeacherIdsHashSet) { Teacher t = context.Teachers.Find(id); context.Teachers.Remove(t); } context.SaveChanges(); #endregion Deleting from db #region Add or update rows in tables of db foreach (DataRow row in cache.Rows) { object[] objs = row.ItemArray; if ((int)objs[1] > 0) { var t = context.Teachers.Find((int)objs[1]); t.Fio = objs[2].ToString(); t.Publications = (int)objs[3]; t.DesiredWageRate = (decimal)objs[4]; context.Entry(t).State = System.Data.Entity.EntityState.Modified; } else { Teacher t = new Teacher() { Fio = objs[2].ToString(), Publications = (int)objs[3], DesiredWageRate = (decimal)objs[4] }; context.Teachers.Add(t); } context.SaveChanges(); #endregion Add or update rows in tables of db } }
public void saveCacheIntoDb() { #region Deleting from db var cacheGroupSubjectIdsHashSet = new HashSet <int>(cache.Rows.OfType <DataRow>() .Select(dr => dr.Field <int>("GroupSubjectId")).ToArray <int>()); var dbGroupSubjectIdsHashSet = new HashSet <int>(context.GroupSubjects.Select(gs => gs.Id).ToArray <int>()); dbGroupSubjectIdsHashSet.ExceptWith(cacheGroupSubjectIdsHashSet); foreach (int id in dbGroupSubjectIdsHashSet) { GroupSubject gs = context.GroupSubjects.Find(id); Subject sub = gs.Subject; GroupOfStudents gr = gs.GroupOfStudents; context.GroupSubjects.Remove(gs); context.Subjects.Remove(sub); context.GroupOfStudents.Remove(gr); } context.SaveChanges(); #endregion Deleting from db #region Add or update rows in tables of db foreach (DataRow row in cache.Rows) { object[] objs = row.ItemArray; if ((int)objs[1] > 0) { var gs = context.GroupSubjects.Find((int)objs[1]); var gr = gs.GroupOfStudents; var sub = gs.Subject; sub.Name = objs[5].ToString(); sub.LectureQty = (int)objs[11]; sub.SeminarQty = (int)objs[12]; sub.LaboratoryQty = (int)objs[13]; sub.LectureCreditQty = (decimal)objs[14]; sub.SeminarCreditQty = (decimal)objs[15]; sub.LaboratoryCreditQty = (decimal)objs[16]; sub.OtherCreditQty = (decimal)objs[17]; sub.AllCreditQty = (decimal)objs[18]; context.Entry(sub).State = System.Data.Entity.EntityState.Modified; gr.SpecialtyId = (int)objs[2]; gr.DepartmentId = (int)objs[6]; gr.Year = (int)objs[8]; gr.Semester = (int)objs[9]; gr.Contingent = (int)objs[10]; context.Entry(gr).State = System.Data.Entity.EntityState.Modified; } else { Subject sub = new Subject() { Name = objs[5].ToString(), LectureQty = (int)objs[11], SeminarQty = (int)objs[12], LaboratoryQty = (int)objs[13], LectureCreditQty = (decimal)objs[14], SeminarCreditQty = (decimal)objs[15], LaboratoryCreditQty = (decimal)objs[16], OtherCreditQty = (decimal)objs[17], AllCreditQty = (decimal)objs[18] }; context.Subjects.Add(sub); GroupOfStudents gr = new GroupOfStudents() { SpecialtyId = (int)objs[2], DepartmentId = (int)objs[6], Contingent = (int)objs[10], Year = (int)objs[8], Semester = (int)objs[9] }; context.GroupOfStudents.Add(gr); GroupSubject gs = new GroupSubject() { SubjectId = sub.Id, GroupId = gr.Id }; context.GroupSubjects.Add(gs); } context.SaveChanges(); #endregion Add or update rows in tables of db } }
private void addButton_Click(object sender, EventArgs e) { int GroupSubjectId = (int)this.parent.cache.Rows[iupParams.RowIndex].ItemArray[1]; int TeacherId = (int)this.teacherChoosingComboBox.SelectedValue; Curriculum existed = context.Curriculums.SingleOrDefault(c => c.GroupSubjectId == GroupSubjectId && c.TeacherId == TeacherId); decimal lectureCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbLectureQty.Text) ? "0" : tbLectureQty.Text); decimal seminarCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbSeminarQty.Text) ? "0" : tbSeminarQty.Text); decimal laborCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbLabQty.Text) ? "0" : tbLabQty.Text); decimal educCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbEducCred.Text) ? "0" : tbEducCred.Text); decimal pedagCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbPedagCred.Text) ? "0" : tbPedagCred.Text); decimal gradCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbGradCred.Text) ? "0" : tbGradCred.Text); decimal industCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbIndustCred.Text) ? "0" : tbIndustCred.Text); decimal researchCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbResearchCred.Text) ? "0" : tbResearchCred.Text); decimal memberCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbMemberCred.Text) ? "0" : tbMemberCred.Text); decimal supervCredits = decimal.Parse(string.IsNullOrWhiteSpace(tbSupervCred.Text) ? "0" : tbSupervCred.Text); decimal cacheLectures = (decimal)parent.cache.Rows[iupParams.RowIndex].ItemArray[14]; decimal cacheSeminars = (decimal)parent.cache.Rows[iupParams.RowIndex].ItemArray[15]; decimal cacheLabors = (decimal)parent.cache.Rows[iupParams.RowIndex].ItemArray[16]; decimal cacheOthers = (decimal)parent.cache.Rows[iupParams.RowIndex].ItemArray[17]; Subject subj = context.GroupSubjects.Find(GroupSubjectId).Subject; decimal residual = cacheLectures + oldLectureCredits - lectureCredits; parent.cache.Rows[iupParams.RowIndex][14] = residual; parent.dataGridView1.Rows[iupParams.RowIndex].Cells[10].Value = (decimal)residual; subj.LectureCreditQty = residual; residual = cacheSeminars + oldSeminarCredits - seminarCredits; parent.cache.Rows[iupParams.RowIndex][15] = residual; parent.dataGridView1.Rows[iupParams.RowIndex].Cells[11].Value = (decimal)residual; subj.SeminarCreditQty = residual; residual = cacheLabors + oldLaboratoryCredits - laborCredits; parent.cache.Rows[iupParams.RowIndex][16] = residual; parent.dataGridView1.Rows[iupParams.RowIndex].Cells[12].Value = (decimal)residual; subj.LaboratoryCreditQty = residual; residual = cacheOthers + oldOtherCredits - educCredits - pedagCredits - gradCredits - industCredits - researchCredits - memberCredits - supervCredits; if (residual < 0) { this.errorProvider1.SetError(addButton, "Значение введенных кредитов превышает максимальное значение дополнительных кредитов"); } subj.OtherCreditQty = residual; context.Entry(subj).State = System.Data.Entity.EntityState.Modified; if (existed == null) { context.Curriculums.Add(new Curriculum() { GroupSubjectId = GroupSubjectId, TeacherId = TeacherId, LectureCredits = lectureCredits, SeminarCredits = seminarCredits, LaboratoryCredits = laborCredits, EducationalPractice = educCredits, PedagogicalPractice = pedagCredits, UndergraduatePractice = gradCredits, IndustrialPractice = industCredits, ResearchPractice = researchCredits, СommissionMembership = memberCredits, SupervisoryWork = supervCredits }); } else { existed.LectureCredits = lectureCredits; existed.SeminarCredits = seminarCredits; existed.LaboratoryCredits = laborCredits; existed.EducationalPractice = educCredits; existed.PedagogicalPractice = pedagCredits; existed.UndergraduatePractice = gradCredits; existed.IndustrialPractice = industCredits; existed.ResearchPractice = researchCredits; existed.СommissionMembership = memberCredits; existed.SupervisoryWork = supervCredits; decimal all = SumUpAllCredits(); if (all == 0) { context.Entry(existed).State = System.Data.Entity.EntityState.Deleted; } } context.SaveChanges(); parent.dataGridView1.Update(); this.Close(); }