There is a tutor, a group and a student.

A tutor supervises several groups and students; if a student is in a group of a tutor, then he is a student of this tutor.

The student is trained at several tutors and consists of several groups. A student can learn from a tutor, but not be in any group.

The group is supervised by several tutors and includes many students.

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; } 

First of all, here the cyclic dependencies are completely: student.tutors[i].students[j].tutors[i].students[j]...

Secondly, when I try to add a new group, the error falls, the place is noted in the code

Group create / update method

  public override Group Create(Group obj) { Group dbEntry = dbContext.Groups.Include("Journals").Include("Students").Include("Tutors").Where(g => g.ID == obj.ID).FirstOrDefault(); if(dbEntry != null) { var deletedTutors = dbEntry.Tutors.Except(obj.Tutors, tutor => tutor.ID).ToList<Tutor>(); var addedTutors = obj.Tutors.Except(dbEntry.Tutors, tutor => tutor.ID).ToList<Tutor>(); deletedTutors.ForEach(tutor=> dbEntry.Tutors.Remove(tutor)); foreach (Tutor tutor in addedTutors) { dbEntry.Tutors.Add(tutor); } var deletedStudents = dbEntry.Students.Except(obj.Students, student => student.ID).ToList<Student>(); var addedStudents = obj.Students.Except(dbEntry.Students, student => student.ID).ToList<Student>(); deletedStudents.ForEach(student => dbEntry.Students.Remove(student)); foreach (Student student in addedStudents) { dbEntry.Students.Add(student); } //if student below to group, he automatticaly belows to all tutors that are owners of group foreach(var student in dbEntry.Students) { var addedTutorsToStudent = dbEntry.Tutors.Except(student.Tutors, tutor => tutor.ID).ToList<Tutor>(); foreach(var tutor in addedTutorsToStudent) { student.Tutors.Add(tutor); } } dbEntry.Name = obj.Name; } else { dbContext.Groups.Add(obj); //!!!Здесь ошибка!!! } dbContext.SaveChanges(); return obj; } 

This is how I create an object of type Group

 public ActionResult Save(int tutorId) { groupForSave.Tutors.Add(tutorRepository.GetById(tutorId)); groupRepository.Create(groupForSave);//что приводит к ошибке в методе Create см.выше } 

MISTAKE

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

    0