zzzprojects / EntityFramework-Extensions

Entity Framework Bulk Operations | Improve Entity Framework performance with Bulk SaveChanges, Insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL and SQLite.
https://entityframework-extensions.net
346 stars 57 forks source link

BulkInsert: System.IndexOutOfRangeException: 'There is no row at position 0.' #571

Open StanlyLife opened 9 months ago

StanlyLife commented 9 months ago

Description

I am trying to BulkInsert a lot of rows in a table on a schedule. Whenever hangfire first executes the function, everything runs smoothly. However second time I try to bulkinsert, i get this error:

System.IndexOutOfRangeException: 'There is no row at position 0.'

Code

        public void AddRange(List<CardVehiclesUsedPo> cvu)
        {
            db.BulkInsert(
                cvu,
                options =>
                {
                    options.InsertIfNotExists = true;
                    options.AllowDuplicateKeys = true;
                    options.ColumnPrimaryKeyExpression = p =>
                        new
                        {
                            p.DriverCardId,
                            p.VehicleOdometerBegin,
                            p.VehicleRegistrationNumber
                        };
                }
            );
            db.SaveChanges();
        }

Data

The data i am trying to add:

[
        {
            "VehicleOdometerBegin": 101841,
            "VehicleOdometerEnd": 102017,
            "VehicleFirstUse": 1643448024,
            "VehicleLastUse": 1643487609,
            "VehicleRegistrationNation": 37,
            "VehicleRegistrationNumber": "REDACTED",
            "VuDataBlockCounter": 0,
            "VehicleIdentificationNumber": "REDACTED"
        },
        {
            "VehicleOdometerBegin": 102621,
            "VehicleOdometerEnd": 102753,
            "VehicleFirstUse": 1643890040,
            "VehicleLastUse": 1643920250,
            "VehicleRegistrationNation": 37,
            "VehicleRegistrationNumber": "REDACTED",
            "VuDataBlockCounter": 0,
            "VehicleIdentificationNumber": "REDACTED"
        }...

Database

image

Exception

If you are seeing an exception, include the full exceptions details (message and stack trace). image

System.IndexOutOfRangeException: 'There is no row at position 0.'
Exception message:
Stack trace:

System.IndexOutOfRangeException
There is no row at position 0.
   at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
   at System.Data.DataRowCollection.get_Item(Int32 index)
   at ?.?(? ?, DbCommand ?)
   at ?.?(? ?, DbCommand ?)
   at ?.Execute(List`1 actions)
   at ?.?(List`1 ?)
   at Z.BulkOperations.BulkOperation.Execute()
   at Z.BulkOperations.BulkOperation.BulkInsert()
   at ?.BulkInsert[T](DbContext this, IEntityType entityType, IEnumerable`1 list, Action`1 options, SavingSelector savingSelector, Boolean forceSpecificTypeMapping, Boolean isOptimized)
   at ?.BulkInsert[T](DbContext this, IEnumerable`1 entities, Action`1 options, Boolean isBulkSaveChanges, Boolean isOptimized)
   at DbContextExtensions.BulkInsert[T](DbContext this, IEnumerable`1 entities, Action`1 options)
   at Redacted.Context.ContextHandlers.CardVehiclesUsedCh.AddRange(List`1 cvu) in C:\Users\StianHåve\Videos\REPOS\Redacted\Context\ContextHandlers\CardVehiclesUsedCh.cs:line 40
   at Redacted.Services.DriverFileService.UploadCardVehiclesUsed(DriverFileInfo file) in C:\Users\StianHåve\Videos\REPOS\Redacted\Services\DriverFileService.cs:line 189
   at Redacted.Services.DriverFileService.Test() in C:\Users\StianHåve\Videos\REPOS\Redacted\Services\DriverFileService.cs:line 66
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Further technical details

JonathanMagnan commented 9 months ago

Hello @StanlyLife ,

Thank you for reporting, we will try to reproduce it with your data.

Best Regards,

Jon

StanlyLife commented 9 months ago

I’m out of the office. But is it possible to share the code & database privately?

Upon further investigation I found that this problem only happened to certain json files and data, but there is no clear indication on what’s causing the error as the models are the same.

JonathanMagnan commented 9 months ago

Hello @StanlyLife ,

You can send your project here: info@zzzprojects.com (or talk more privately to us at this mailbox).

In this case, we will surely wait for your project, which will make it easier for us to reproduce it.

Best Regards,

Jon

StanlyLife commented 9 months ago

Perfect, i shared the repo and instructions to the email you provided. Looking forward to hearing from you!