public void AddSubject(Subject subject, List <int> subjectConditionIDs, List <int> AccountIDs) { pbDB = new ProgramBuilderEntities(); subject.CreationTime = DateTime.Now; pbDB.Subjects.Add(subject); pbDB.SaveChanges(); int CurrentSubjectID = subject.ID; Subject CurrentSub = pbDB.Subjects.SingleOrDefault(x => x.ID == CurrentSubjectID); for (int i = 0; i < AccountIDs.Count; i++) { if (AccountIDs[i] == AccountIDs[AccountIDs.Count - 1]) { AccountIDs.RemoveAt(i); break; } } for (int i = 0; i < AccountIDs.Count; i++) { int tempAccID = AccountIDs[i]; SubjectsAccount SubAcc = new SubjectsAccount(); var acc = (from Acc2 in pbDB.Accounts where Acc2.ID == tempAccID select Acc2).SingleOrDefault(); SubAcc.Subject = CurrentSub; SubAcc.Account = acc; SubAcc.IsSyllabusEditor = false; if (i == AccountIDs.Count - 1) { SubAcc.IsSyllabusEditor = true; } pbDB.SubjectsAccounts.Add(SubAcc); } //if (AccountIDs != null) //{ // foreach (int item in AccountIDs) // { // if (item != AccountID) // { // NewAccountIDs.Add(item); // } // } //} //NewAccountIDs.Add(AccountID); //foreach(int item in NewAccountIDs) //{ // Account acc = (from Acc2 in pbDB.Accounts // where Acc2.ID == item // select Acc2).Single(); // SubjectsAccount SubAcc = new SubjectsAccount(); // SubAcc.Account = acc; // SubAcc.Subject = CurrentSub; // SubAcc.IsSyllabusEditor = false; // if (item.Equals(AccountID)) // { // SubAcc.IsSyllabusEditor = true; // } // pbDB.SubjectsAccounts.Add(SubAcc); //} if (subjectConditionIDs != null) { foreach (int subjectConditionID in subjectConditionIDs) { Subject relateSub = pbDB.Subjects.SingleOrDefault(x => x.ID == subjectConditionID); CurrentSub.Subjects1.Add(relateSub); } } pbDB.SaveChanges(); }
public void EditSubject(Subject subject, List <int> AccountIDs, List <int> subjectConditionIDs) { pbDB = new ProgramBuilderEntities(); Subject CurrentSubj = pbDB.Subjects.SingleOrDefault(x => x.ID == subject.ID); CurrentSubj.Name = subject.Name; CurrentSubj.PartialCode = subject.PartialCode; CurrentSubj.CreditNumber = subject.CreditNumber; CurrentSubj.TheoryNumber = subject.TheoryNumber; CurrentSubj.PracticeNumber = subject.PracticeNumber; CurrentSubj.LearningLevel = subject.LearningLevel; CurrentSubj.SemesterNumber = subject.SemesterNumber; CurrentSubj.Description = subject.Description; CurrentSubj.Purpose = subject.Purpose; CurrentSubj.SubjectTypeID = subject.SubjectTypeID; CurrentSubj.FacultyID = subject.FacultyID; CurrentSubj.Form = subject.Form; //Remove CurrentSubj.SubjectsAccounts.Clear(); CurrentSubj.Subjects1.Clear(); // Add for (int i = 0; i < AccountIDs.Count; i++) { if (AccountIDs[i] == AccountIDs[AccountIDs.Count - 1]) { AccountIDs.RemoveAt(i); break; } } for (int i = 0; i < AccountIDs.Count; i++) { int tempAccID = AccountIDs[i]; SubjectsAccount SubAcc = new SubjectsAccount(); var acc = (from Acc2 in pbDB.Accounts where Acc2.ID == tempAccID select Acc2).SingleOrDefault(); SubAcc.Subject = CurrentSubj; SubAcc.Account = acc; SubAcc.IsSyllabusEditor = false; if (i == AccountIDs.Count - 1) { SubAcc.IsSyllabusEditor = true; } pbDB.SubjectsAccounts.Add(SubAcc); } if (subjectConditionIDs != null) { foreach (int subjectConditionID in subjectConditionIDs) { Subject relateSub = pbDB.Subjects.SingleOrDefault(x => x.ID == subjectConditionID); CurrentSubj.Subjects1.Add(relateSub); } } pbDB.SaveChanges(); }