dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

Index was outside the bounds of the array while saving changes #32538

Closed caothuong closed 9 months ago

caothuong commented 9 months ago

VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21, @p22, @p23, @p24, @p25, @p26, @p27, @p28, @p29, @p30, @p31, @p32, @p33, @p34, @p35, @p36, @p37, @p38, @p39, @p40, @p41, @p42, @p43, @p44, @p45, @p46, @p47, @p48, @p49, @p50, @p51, @p52, @p53, @p54, @p55, @p56, @p57, @p58, @p59, @p60, @p61, @p62, @p63, @p64, @p65, @p66, @p67, @p68, @p69, @p70, @p71, @p72, @p73, @p74, @p75, @p76, @p77, @p78, @p79, @p80, @p81, @p82, @p83, @p84, @p85, @p86, @p87, @p88, @p89, @p90, @p91, @p92, @p93, @p94, @p95, @p96, @p97, @p98, @p99, @p100, @p101, @p102, @p103, @p104, @p105, @p106, @p107, @p108, @p109, @p110, @p111, @p112, @p113); SELECT [VEHICLE_KEY], [BMW_CONNECTEDDRIVE_CAPABLE], [CAN_UPDATE_EXTERNALLY], [IS_FIRST_MOT], [IS_NON_AUSTRALIA_IMPORT], [IS_ODOMETER_IN_MILES_VISIBLE], [IS_VINLINK_CLEANSED] FROM [VEHICLE] WHERE @@ROWCOUNT = 1 AND [VEHICLE_KEY] = scope_identity(); fail: Microsoft.EntityFrameworkCore.Update[10000] An exception occurred in the database while saving changes for context type 'Core.Infrastructure.Models.DealerContext'. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at Microsoft.Data.SqlClient.SqlDataReader.CheckDataIsReady(Int32 columnIndex, Boolean allowPartiallyReadColumn, Boolean permitAsync, String methodName) at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i) at Microsoft.EntityFrameworkCore.RelationalPropertyExtensions.GetReaderFieldValue(IProperty property, RelationalDataReader relationalReader, Int32 ordinal, Boolean detailedErrorsEnabled) at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateResults(RelationalDataReader relationalReader) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetAsync(Int32 startCommandIndex, RelationalDataReader reader, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken) --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.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) at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList1 entriesToSave, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at Microsoft.Data.SqlClient.SqlDataReader.CheckDataIsReady(Int32 columnIndex, Boolean allowPartiallyReadColumn, Boolean permitAsync, String methodName) at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i) at Microsoft.EntityFrameworkCore.RelationalPropertyExtensions.GetReaderFieldValue(IProperty property, RelationalDataReader relationalReader, Int32 ordinal, Boolean detailedErrorsEnabled) at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateResults(RelationalDataReader relationalReader) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetAsync(Int32 startCommandIndex, RelationalDataReader reader, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken) --- End of inner exception stack trace ---

roji commented 9 months ago

@caothuong it seems that you're using a trigger on the table. This kind of error sometimes shows up when the trigger modifies the data that gets returned from the database; EF expects the specific information it requests for to come back from the database, and interfering with that in any way (in a trigger) can cause this failure.

Try to temporarily remove your trigger to see if the problem persists. If the problem goes away, you'll need to fix your trigger to not interfere with the resultset.

Otherwise, you'll need to post a minimal, runnable code sample to allow us to reproduce the bug.

caothuong commented 9 months ago

@roji Yes, I'm using some trigger after update/insert vehicle table. So we have any way to handle it.

roji commented 9 months ago

@caothuong EF needs to load back the columns that were generated in the database after the INSERT, that's what this does:

SELECT [VEHICLE_KEY], [BMW_CONNECTEDDRIVE_CAPABLE], [CAN_UPDATE_EXTERNALLY], [IS_FIRST_MOT], [IS_NON_AUSTRALIA_IMPORT], [IS_ODOMETER_IN_MILES_VISIBLE], [IS_VINLINK_CLEANSED]
FROM [VEHICLE]

You have to ensure that the data resulting from the INSERT corresponds to that, possibly modifying your trigger accordingly.

caothuong commented 9 months ago

Hi @roji , thanks a lot for your response. So Where I can modify the columns on EF to return instead this: SELECT [VEHICLE_KEY], [BMW_CONNECTEDDRIVE_CAPABLE], [CAN_UPDATE_EXTERNALLY], [IS_FIRST_MOT], [IS_NON_AUSTRALIA_IMPORT], [IS_ODOMETER_IN_MILES_VISIBLE], [IS_VINLINK_CLEANSED] FROM [VEHICLE]

roji commented 9 months ago

@caothuong the problem is with your trigger, not with EF; whatever you're doing in your trigger, it's causing something to be returned from the database that's other than the columns EF expects.

caothuong commented 9 months ago

@roji, thank you so much. I fixed it.