C
C#2w ago
!Rushaan

Collection was modified. Enumeration operation may not execute.

Code:
public async Task<bool> DeleteFolder(Folder folder)
{
if (folder.SubFolders.Any())
{
foreach (Folder f in folder.SubFolders)
{
await DeleteFolder(f);
}
}

if (folder.Files.Any())
{
foreach (Core.Domain.Entities.File file in folder.Files)
{
DisconnectAndNullifyMetadataAndSharing(file);
_db.Files.Remove(file);
}
}

folder.ParentFolder = null;
folder.ParentFolderId = null;

DisconnectAndNullifyMetadataAndSharing(folder);
_db.Folders.Remove(folder);

return await _db.SaveChangesAsync() > 0;
}

// Utility Function
private void DisconnectAndNullifyMetadataAndSharing(BaseForFileFolder entity)
{
if (entity.Metadata != null)
{
Metadata metadata = entity.Metadata;
metadata.File = null;

entity.Metadata = null;
entity.MetadataId = null;
}
if (entity.Sharing != null)
{
Sharing sharing = entity.Sharing;
sharing.File = null;

entity.Sharing = null;
entity.SharingId = null;
}
}
public async Task<bool> DeleteFolder(Folder folder)
{
if (folder.SubFolders.Any())
{
foreach (Folder f in folder.SubFolders)
{
await DeleteFolder(f);
}
}

if (folder.Files.Any())
{
foreach (Core.Domain.Entities.File file in folder.Files)
{
DisconnectAndNullifyMetadataAndSharing(file);
_db.Files.Remove(file);
}
}

folder.ParentFolder = null;
folder.ParentFolderId = null;

DisconnectAndNullifyMetadataAndSharing(folder);
_db.Folders.Remove(folder);

return await _db.SaveChangesAsync() > 0;
}

// Utility Function
private void DisconnectAndNullifyMetadataAndSharing(BaseForFileFolder entity)
{
if (entity.Metadata != null)
{
Metadata metadata = entity.Metadata;
metadata.File = null;

entity.Metadata = null;
entity.MetadataId = null;
}
if (entity.Sharing != null)
{
Sharing sharing = entity.Sharing;
sharing.File = null;

entity.Sharing = null;
entity.SharingId = null;
}
}
Error:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at CloudStoragePlatform.Infrastructure.Repositories.FoldersRepository.DeleteFolder(Folder folder) in C:\Users\rusha\source\repos\Cloud Storage Platform\CloudStoragePlatform.Infrastructure\Repositories\FoldersRepository.cs:line 135
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at CloudStoragePlatform.Infrastructure.Repositories.FoldersRepository.DeleteFolder(Folder folder) in C:\Users\rusha\source\repos\Cloud Storage Platform\CloudStoragePlatform.Infrastructure\Repositories\FoldersRepository.cs:line 135
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
surf68
surf682w ago
presumably it's the call to DeleteFolder while iterating over folder.SubFolders
sibber
sibber2w ago
is that ef core?
MasterSubarashii
when you delete "f" inside folder.Subfolders it causes it i guess
sibber
sibber2w ago
you cant modify a collection while enumerating it
!Rushaan
!RushaanOP2w ago
yea
MasterSubarashii
just assign folder.Subfolders to another variable and iterate over that while keeping the folders parameter like that
surf68
surf682w ago
the easy solution would be to iterate over folder.SubFolders.ToList() to bring a copy of the collection into memory
!Rushaan
!RushaanOP2w ago
oh, the line the error is pointing to seems to be the first foreach loop
MasterSubarashii
yep like the surf and sibber said
!Rushaan
!RushaanOP2w ago
okay, EF Core will still track the changes right?
surf68
surf682w ago
yes, you're still operating on the same instances of the subfolder, you're just iterating over a different collection.
!Rushaan
!RushaanOP2w ago
alr imma try it damn it worked tysm imma test it a lil more, yep its fully working
Want results from more Discord servers?
Add your server