public static void addSubjectSemester(CSemesterEntityCSubjectsEntity takeDataSemesterSubject, int iInstructorId) { dbContext = new StudentPlanManagementContext(); takeDataSemesterSubject.Instructor = dbContext.InstructorEntities.SingleOrDefault(intr => intr.InstructorId == iInstructorId); dbContext.SubjectsTakesEntities.Add(takeDataSemesterSubject); dbContext.SaveChanges(); //An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details. do dax co tren databese luu trungf lai }
private void btnSave_Click(object sender, EventArgs e) { int iInstructorId = listInstructors[cmbInstructor.SelectedIndex].InstructorId; CSemesterEntityCSubjectsEntity takeDataSemesterSubject = new CSemesterEntityCSubjectsEntity { StudentId = strStudentId, SubjectId = listSubjects[cmbSubject.SelectedIndex].SubjectId, SemesterId = strSemesterId, DateOfTake = DateTime.Now, //Instructor = listInstructors[cmbInstructor.SelectedIndex] lấy vầy tại sao lỗi An entity object cannot be referenced by multiple instances of IEntityChangeTracker. //nhưng bỏ nó null không lỗi hoặc lấy như thế này không lỗi takeDataSemesterSubject.Instructor = context.InstructorEntities.SingleOrDefault(s => s.InstructorId == sss); // An error occurred while updating the entries. See the inner exception for details. do trùng khóa chính }; #region Tại sao như vầy lại lỗi Additional information: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details. //CSemesterEntityCSubjectsEntity takeDataSemesterSubject = new CSemesterEntityCSubjectsEntity(); //takeDataSemesterSubject.SemesterId = strStudentId; //takeDataSemesterSubject.SubjectId = listSubjects[cmbSubject.SelectedIndex].SubjectId; //takeDataSemesterSubject.SemesterId = strSemesterId; //takeDataSemesterSubject.DateOfTake = DateTime.Now; //takeDataSemesterSubject.Instructor = listInstructors[cmbInstructor.SelectedIndex]; #endregion //StudentPlanManagementBusiness.CTakeSubjectBusiness.addSubjectSemester(takeDataSemesterSubject, iInstructorId); //ddang ky moon hoc gop lai lun, khi gop lai nhuoc diem nhap nhanwf nhung no se the hien thuc the dang ky Take la ao, quan trong Schedule hon bool firstDaySchedule = true; CScheduleEntity scheduleData; #region Decryption WeekDays int valueWeekDay = weekDayStudy.WeekDays.GetHashCode(); if (valueWeekDay > 63) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; #endregion scheduleData.WeekOfDay = "Thứ bảy"; if (firstDaySchedule) { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); firstDaySchedule = false; } else { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleNonRegisterSubjectSemester(scheduleData, takeDataSemesterSubject); } valueWeekDay -= 64; } if (valueWeekDay > 31) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; #endregion scheduleData.WeekOfDay = "Thứ sáu"; StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); valueWeekDay -= 32; } if (valueWeekDay > 15) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; scheduleData.Take = takeDataSemesterSubject; #endregion scheduleData.WeekOfDay = "Thứ năm"; if (firstDaySchedule) { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); firstDaySchedule = false; } else { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleNonRegisterSubjectSemester(scheduleData, takeDataSemesterSubject); } valueWeekDay -= 16; } if (valueWeekDay > 7) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; #endregion scheduleData.WeekOfDay = "Thứ tư"; if (firstDaySchedule) { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); firstDaySchedule = false; } else { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleNonRegisterSubjectSemester(scheduleData, takeDataSemesterSubject); } valueWeekDay -= 8; } if (valueWeekDay > 3) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; scheduleData.Take = takeDataSemesterSubject; #endregion scheduleData.WeekOfDay = "Thứ ba"; if (firstDaySchedule) { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); firstDaySchedule = false; } else { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleNonRegisterSubjectSemester(scheduleData, takeDataSemesterSubject); } valueWeekDay -= 4; } if (valueWeekDay > 1) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; #endregion scheduleData.WeekOfDay = "Thứ hai"; if (firstDaySchedule) { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); firstDaySchedule = false; } else { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleNonRegisterSubjectSemester(scheduleData, takeDataSemesterSubject); } valueWeekDay -= 2; } if (valueWeekDay > 0) { #region NewSchedule scheduleData = new CScheduleEntity(); scheduleData.ScheduleId = "S" + StudentPlanManagementBusiness.CScheduleBusiness.countScheduleInTable(); scheduleData.ScheduleTitle = txtScheduleTitle.Text; scheduleData.TimeStart = tmeStart.Time.TimeOfDay; scheduleData.TimeEnd = tmeEnd.Time.TimeOfDay; scheduleData.ClassRom = txtClassRom.Text; #endregion scheduleData.WeekOfDay = "Chủ nhật"; if (firstDaySchedule) { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleRegisterSubjectSemester(scheduleData, takeDataSemesterSubject, iInstructorId); firstDaySchedule = false; } else { StudentPlanManagementBusiness.CScheduleBusiness.addScheduleNonRegisterSubjectSemester(scheduleData, takeDataSemesterSubject); } valueWeekDay -= 1; } #endregion this.Close(); }