Skip to content

Can't map NpgsqlRange<OffsetDateTime> to tstzrange #2058

Closed

Description

@thomasdc

Problem description

I'm trying to save an entity with a property of type NpgsqlRange<OffsetDateTime> using Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime and get the following exception on SaveChangesAsync:

Unhandled exception. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
 ---> System.InvalidCastException: Can't write CLR type NpgsqlTypes.NpgsqlRange`1[NodaTime.OffsetDateTime] with handler type RangeHandler`1
   at lambda_method53(Closure , NpgsqlTypeHandler , Object , NpgsqlLengthCache& , NpgsqlParameter )
   at Npgsql.TypeHandling.NpgsqlTypeHandler`1.ValidateObjectAndGetLength(Object value, NpgsqlLengthCache& lengthCache, NpgsqlParameter parameter)
   at Npgsql.NpgsqlParameter.ValidateAndGetLength()
   at Npgsql.NpgsqlCommand.ValidateParameters(ConnectorTypeMapper typeMapper)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, 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)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at npgsql_nodatime_efcore_poc.Program.Main(String[] args) in C:\sources_ref\npgsql_nodatime_efcore_poc\Program.cs:line 26
   at npgsql_nodatime_efcore_poc.Program.Main(String[] args) in C:\sources_ref\npgsql_nodatime_efcore_poc\Program.cs:line 26
   at npgsql_nodatime_efcore_poc.Program.<Main>(String[] args)

Version

  • Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime: 5.0.10
  • (I get the same exception when using 6.0.0-rc2)

Reproducible demo

Activity

  1. changed the title [-]DbUpdateException when saving property of type NpgsqlRange<OffsetDateTime>[/-] [+]DbUpdateException when saving property of type `NpgsqlRange<OffsetDateTime>`[/+] on Oct 25, 2021
  2. self-assigned this
    on Oct 25, 2021
  3. added this to the 6.0.0 milestone on Oct 25, 2021
  4. roji commented on Oct 25, 2021

    @roji
    Member

    Thanks, I can indeed see this happening and will fix it for 6.0.

    Note that there's also the NodaTime Interval which seems perfect for tstzrange - opened #4070 to track mapping that as well (I'd recommend going with that rather than NpgsqlRange<OffsetDateTime>).

  5. changed the title [-]DbUpdateException when saving property of type `NpgsqlRange<OffsetDateTime>`[/-] [+]Can't map NpgsqlRange<OffsetDateTime> to tstzrange[/+] on Oct 26, 2021
  6. linked a pull request that will close this issueMap NodaTime Interval to tstzrange #2060on Oct 26, 2021
  7. roji commented on Oct 26, 2021

    @roji
    Member

    @thomasdc this is fixed in the latest daily build (6.0.0-rtm-ci.20211026T135606 and above in the vNext feed). It would be good if you could give this a try and confirm everything is working well.

  8. thomasdc commented on Oct 27, 2021

    @thomasdc
    Author

    I can confirm it works on the latest nuget. Thanks!
    Any chance this might get hotfixed on net5.0 as well? We haven't committed yet on upgrading to net6.0.
    In the meantime, I noticed NpgsqlRange<Instant> does work, so we'll use that one for the time being.

  9. roji commented on Oct 27, 2021

    @roji
    Member

    @thomasdc unfortunately it won't be possible to backport this - range support has been improved in many ways in 6.0, and doing this for 5.0 may get complicated. But NpgsqlRange<Instant> should be a good alternative.

  10. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions