Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V6 NpgsqlModificationCommandBatch.ConsumeAsync commandIndex Out of Range #3080

Open
trionia opened this issue Feb 6, 2024 · 0 comments
Open

Comments

@trionia
Copy link

trionia commented Feb 6, 2024

Applies only for version 6
I've stumbled across it a while ago, when my entity didn't match my table schema, but the exception was somehow cryptic.

After a bit of research I've found the following:

var modificationCommand = ModificationCommands[commandIndex++];

If anything throws after that, like ReadAsync, and the current command is the last, the catch (Exception ex) block will throw IndexOutOfRangeException instead.
catch (Exception ex)
{
throw new DbUpdateException(
RelationalStrings.UpdateStoreException,
ex,
ModificationCommands[commandIndex].Entries);
}

The solution could look like this:

diff --git a/src/EFCore.PG/Update/Internal/NpgsqlModificationCommandBatch.cs b/src/EFCore.PG/Update/Internal/NpgsqlModificationCommandBatch.cs
index 2d6ec3bb..ef00c788 100644
--- a/src/EFCore.PG/Update/Internal/NpgsqlModificationCommandBatch.cs
+++ b/src/EFCore.PG/Update/Internal/NpgsqlModificationCommandBatch.cs
@@ -197,7 +197,7 @@ protected override void Consume(RelationalDataReader reader)

                 // Extract result from the command and propagate it

-                var modificationCommand = ModificationCommands[commandIndex++];
+                var modificationCommand = ModificationCommands[commandIndex];

                 if (!(await reader.ReadAsync(cancellationToken).ConfigureAwait(false)))
                 {
@@ -210,6 +210,8 @@ protected override void Consume(RelationalDataReader reader)
                 var valueBufferFactory = CreateValueBufferFactory(modificationCommand.ColumnModifications);
                 modificationCommand.PropagateResults(valueBufferFactory.Create(npgsqlReader));

+                commandIndex++;
+
                 await npgsqlReader.NextResultAsync(cancellationToken).ConfigureAwait(false);
             }
         }

It is similar to version 7

command.PropagateResults(reader);
commandIndex++;
onResultSet = async
? await npgsqlReader.NextResultAsync(cancellationToken).ConfigureAwait(false)
: npgsqlReader.NextResult();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant