ArgumentOutOfRangeException for query on CockroachDB?
I have an asp.net project with a simple INSERT query, which is confirmed to work with Postgres. Replacing Postgres with CockroachDB breaks it, however:
Executed DbCommand (5ms) [Parameters=[@p0='1d808b9a-6cb3-48b6-b5a3-961f98e941b9' (Nullable = false), @p1='2021-02-06T22:28:01.3495134+00:00' (DbType = DateTime), @p2='[email protected]' (Nullable = false), @p3='2021-02-06T22:28:01.3495134+00:00' (DbType = DateTime)], CommandType='Text', CommandTimeout='30']
INSERT INTO "Accounts" ("MyIdentityName", "Created", "Email", "LastChanged")
VALUES (@p0, @p1, @p2, @p3)
RETURNING "Id";
An exception occurred in the database while saving changes for context type 'KlusterManager.Data.DataMainDbContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
There is an issue thats related to this i think, on the Cockroach repo: https://github.com/cockroachdb/cockroach/issues/48050
In that issue its mentioned that "The above can be worked around in the driver, i.e. by disabling the specific features (i.e. catching exceptions and moving on)." - i dont know if thats reasonable or not
Cross-posted on stackoverflow: https://stackoverflow.com/questions/66082962/entity-framework-argumentoutofrangeexception-for-query-on-cockroachdb
/cc @rafiss
@mgj please post a runnable, minimal console program which reproduces - an exception isn't sufficient to properly investigate what's going on.
@roji Thanks for your reply
I have created a minimal console application that reproduces the issue here: https://github.com/RandomStuffAndCode/Cockroachdb_postgres_issue
It uses different DbContext implementations to switch between a local Postgres installation and a local CockroachDB installation. Using DataMainDbContextPostgres works, DataMainDbContextCockroach does not.
I included some protobuf annotations on the Account model to make it a more realistic example, but they dont seem to have any effect on the issue
Thanks! From what I can see the error message is not coming from within CockroachDB, so I'm not familiar with it. But if it turns out that the issue is CockroachDB supplying unexpected data/metadata, I will track this down further.
@rafiss Thanks for your input. Its odd that postgres works, but cockroachdb doesnt, but you guys probably know how to locate the issue better than i do
If there is anything i can do, any more information i can provide, please let me know
Unfortunately I don't really have time (or knowledge) for setting up CockroachDB and diving into this... If someone can give a hand that would be great.
I can try giving it a shot later, but I don't have a .NET environment easily available. Hahah, if only we could merge our dev environments together.. :)
FYI, CockroachDB is easy to start locally as a simple binary without any config setup. It's available as a Docker image or executable for common platforms here: https://www.cockroachlabs.com/docs/releases/ then the command cockroach start-single-node --insecure starts a server on port 26257 with a root user and no password.
Hi guys, i had the same issue.

But, there's something really strange... look at this:


The original exception is not being thrown, cause an exception happens when it's trying to access the ModificationCommands.
So, i'm getting this error:
Microsoft.EntityFrameworkCore.Update[10000] An exception occurred in the database while saving changes for context type 'Veredas.Data.Context.VeredasDbContext'. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List`1.get_Item(Int32 index) at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
Instead of this:
The value for property 'IsPep' of entity type 'BasePerson' cannot be set to null because its type is 'bool' which is not a nullable type.