BulkInsert: System.IndexOutOfRangeException: 'There is no row at position 0.'
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
Exception
If you are seeing an exception, include the full exceptions details (message and stack trace).
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
- EF version: [EF Core v8.0]
- EF Extensions version: [EFE Core v8.102.0.0]
- Database Server version: [SQL Server 2002]
- Database Provider version (NuGet): [Microsoft.Data.SqlClient v5.1.2]
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" /><PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="8.102.0" />
Hello @StanlyLife ,
Thank you for reporting, we will try to reproduce it with your data.
Best Regards,
Jon
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.
Hello @StanlyLife ,
You can send your project here: [email protected] (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
Perfect, i shared the repo and instructions to the email you provided. Looking forward to hearing from you!