Collection was modified. Enumeration operation may not execute.
Code:
Error:
It seems that the error is being triggered because database is being updated while iterating over the collection of entities in the database or is it happening only because properties of collection are being changed while inside the for loop or because of _db.Folders.Remove(...) ?
Also how can I fix it because at the end of the day I want to modify the collection and delete the rows I wanna delete? I got an idea but I think forgot it
12 Replies
presumably it's the call to
DeleteFolder
while iterating over folder.SubFolders
is that ef core?
when you delete "f" inside folder.Subfolders it causes it i guess
you cant modify a collection while enumerating it
yea
just assign folder.Subfolders to another variable and iterate over that while keeping the folders parameter like that
the easy solution would be to iterate over
folder.SubFolders.ToList()
to bring a copy of the collection into memoryoh, the line the error is pointing to seems to be the first foreach loop
yep
like the surf and sibber said
okay, EF Core will still track the changes right?
yes, you're still operating on the same instances of the subfolder, you're just iterating over a different collection.
alr imma try it
damn it worked tysm
imma test it a lil more, yep its fully working