EFCore.BulkExtensions icon indicating copy to clipboard operation
EFCore.BulkExtensions copied to clipboard

BulkInsert() throw an error when data have Timespan

Open jukbot opened this issue 4 months ago • 0 comments

Description

When using BulkInsertOrUpdate with data containing TimeSpan, if the DTO includes the TimeSpan data type, it breaks the insert. It throws an error due to an incorrect binary data format during copying.

Exception

 Exception data:
          Severity: ERROR
          SqlState: 22P03
          MessageText: incorrect binary data format
          Where: COPY PlansTemp31f04ded, line 1, column lunch_end
          File: copyfromparse.c
          Line: 1984
          Routine: CopyReadBinaryAttribute

Packages

<PackageReference Include="EFCore.BulkExtensions" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />

Example code

var newPlan = new PlanModel
{
    PlanName = planDto.PlanName,
    CourseId = planDto.CourseId,
    LevelId = planDto.LevelId,
    AcademicYear = planDto.AcademicYear,
    LunchStart = TimeSpan.Parse("11:50:00"),
    LunchEnd = TimeSpan.Parse("12:40:00"),
    PeriodDuration = 50
};

var plans = new List<PlanModel> { newPlan };
await _appDbContext.BulkInsertAsync(plans, new BulkConfig { SetOutputIdentity = true });

// Reverted: use regular EF insert for single Plan (avoids PostgreSQL TimeSpan binary COPY issue)
// await _appDbContext.Plans.AddAsync(newPlan);
// await _appDbContext.SaveChangesAsync();

jukbot avatar Aug 14 '25 05:08 jukbot