Skip to content

Problems when attaching new dbcontexts while performing savechanges #63

@bjoka042

Description

@bjoka042

Backstory: I have implemented repositories that store the data. In each of the DbContext models i have attached an auditlog handler to catch changes and log them using another repository.

    public override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
        AuditLog[] logs = null;

        if (_auditLogTracker != null)
            logs = _auditLogTracker.TrackChanges(this);

        var result = base.SaveChanges(acceptAllChangesOnSuccess);

        if (_auditLogTracker != null && logs != null)
            _auditLogTracker.Save(logs);

        return result;
    }

Error: Collection was modified; enumeration operation may not execute.

The error is due to a new context being added while looping through contexts and performing commit.
Could the DbContextCollection perform other than foreach loops to go through the list to allow for new contexts being added during savechanges?

for(var i = 0; i contexts.length; i++)
{
//would probably allow for dynamicly injected contexts
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions