0

So I have a form which hits the update API endpoint when it is submitted. Suppose its a teacher form and the teacher can add, delete or update students. The relationship has been set u in my context class with one teacher to many students.

So, if the id of the student is present in the database, simply update the data. If the Id is 0, meaning no id was passed in since its a new student being created, create a new student model. If the id is not present within the database, delete the student record from the database.

Here's an example: So, assuming the form has 3 students with id 1, 2, 3 and the teacher removes student 2 and adds a new student. It will be one request with { student 1(changed info), student 3, student(no Id) . Since student 2 is missing, student 2 will be deleted from the DB.

So, here's my code:

Code 1

teacher.Students.Clear();

foreach (var student in model.Students)
{
    int id = student.Id;

    if (id != 0)
    {
        Student existingStudent = await _studentRepository.FindAsync(id);
        existingStudent.Name = student.AccountName;
        ... update other student properties
    } else if (id == 0)
    {
        Student newModel = new Student
        {
             Name = student.Name,
             ... other properties
        };

         await _studentRepository.CreateAsync(newModel);
    } 
} 

So in my first attempt, I cleared the students from teachers.Students. Then Im readding bak watever is in the request body bak to the students table. However, when I do this, I get this error:

The association between entity types 'Teacher' and 'Student' has been severed but the relationship is either marked as 'Required' or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, then setup the relationship to use cascade deletes. 

My second attempt is this: 2nd code

foreach (var student in teacher.Students)
{
     studentRepository.DeleteAsync(student);
}

foreach (var student in model.Students)
{
    int id = student.Id;

    if (id != 0)
    {
        Student existingStudent = await _studentRepository.FindAsync(id);
        existingStudent.Name = student.AccountName;
        ... update other student properties
    } else if (id == 0)
    {
        Student newModel = new Student
        {
             Name = student.Name,
             ... other properties
        };

         await _studentRepository.CreateAsync(newModel);
    } 
} 

When I did this, I get this error:

System.InvalidOperationException: Collection was modified after the enumerator was instantiated.

So, what should I be doing to overcome this issue?

Thank you!

jarlh
  • 42,561
  • 8
  • 45
  • 63
Nigel
  • 985
  • 1
  • 11
  • 16
  • should not you await the following: studentRepository.DeleteAsync(student); can we get the line that error happened, you can find it in full stack trace. – Oğuzhan Topçu Feb 19 '20 at 08:31
  • Already Submited Question:-https://stackoverflow.com/questions/844850/strange-collection-was-modified-after-the-enumerator-was-instantiated-exceptio – Malakiya sanjay Feb 19 '20 at 08:31

0 Answers0