There are 3 entities - tutor, student, group, all connected with each other through many to many. I decided to do this: I declare the static variable groupForSave , and in it I put a group from the database or a blank for a new group. When adding a new student, using ajax, I add a new student to the list of groupForSave.Students , while editing, I modify the collectionForSave.Students collection object groupForSave.Students . Virtually all group collections are changed in groupForSave . At the very end, when saving changes, I call Update / Create by passing the generated groupForSave.
Problem I cannot create a new group, see the code, the line at the very beginning, the error drops even though there is not a single student in the group, the only one is a tutor, the other links are empty.
My guesses Perhaps when I take the tutor controller in the Create method from the repository, somehow its context is not destroyed. I tried to replace private dbontext = new EfDbCOntext() in all repositories with using received an error that I try to use an object from a nonexistent context in the same place when adding a group.
//entity framework часть class GroupRepository { dbContext = new EfDbContext(); //Ошика как-то связана с контекстами //упрощено, ошибка при добавлении новой записи public Group Create(Group obj) { dbContext.Groups.Add(obj); //Здесь падает ошибка } } /* ... Классы-репозитории с аналогичным методом Create для остальных сущностей(entity) */ class Group { public virtual List<Tutor> Tutors; public virtual List<Student> Students; } class Student { public virtual List<Group> Groups; public virtual List<Tutor> Tutors; } class Tutor { public virtual List<Group> Groups; public virtual List<Student> Students; } //Контроллер GroupController() { GroupRepository groupRepository = new GroupRepository(); TutorRepository tutorRepository = new TutorRepository(); StudentRepository studentRepository = new StudentRepository(); } [HttpPost] public JsonResult AddStudent(Student student) { //because id is 0 we should define if we need to create new student or it aleady exists in database var stud = studentRepository.GetByMail(student.Email); if(stud != null) { student = stud; }else { student.ID = idCounter++; } groupForSave.Students.Add(student); return Json(student); } public ActionResult Create(int tutorId) { EditGroupViewModel model = new EditGroupViewModel(); groupForSave = new Group(); groupForSave.Journals = new List<Journal>(); groupForSave.Students = new List<Student>(); groupForSave.Tutors = new List<Tutor>(); groupForSave.Tutors.Add(tutorRepository.GetById(tutorId)); model.name = "Новая группа " + groupRepository.GetTutorGroups(tutorId).Count(); model.tutorId = tutorId; redirectAfterCreate = Request.UrlReferrer.ToString(); return View("Edit", model); } public JsonResult Save(Group group) { //return Json(group, JsonRequestBehavior.AllowGet); groupForSave.Name = group.Name; groupRepository.CreateWithContext(groupForSave, tutorRepository.Context); return Json(new { redirectUrl = redirectAfterCreate }, JsonRequestBehavior.AllowGet); }