Repository navigation
Milestone
Description
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)
Activity
[-]DbUpdateException when saving property of type NpgsqlRange<OffsetDateTime>[/-][+]DbUpdateException when saving property of type `NpgsqlRange<OffsetDateTime>`[/+]roji commentedon Oct 25, 2021
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>).[-]DbUpdateException when saving property of type `NpgsqlRange<OffsetDateTime>`[/-][+]Can't map NpgsqlRange<OffsetDateTime> to tstzrange[/+]roji commentedon Oct 26, 2021
@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.
thomasdc commentedon Oct 27, 2021
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.roji commentedon Oct 27, 2021
@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.