You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Writing values of 'NpgsqlTypes.NpgsqlRange`1[[System.TimeSpan, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][]' is not supported for parameters having NpgsqlDbType '-2147483608'.
#3137
Open
NazarPolevyi opened this issue
Mar 24, 2024
· 3 comments
Hi, after Npgsql.EntityFrameworkCore.PostgreSQL update from to 7.0.4 to 8.0.2. We have started to receive an exception when saving into db.
InvalidCastException: Writing values of 'NpgsqlTypes.NpgsqlRange`1[[System.TimeSpan, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][]' is not supported for parameters having NpgsqlDbType '-2147483608'.
public class RestrictionSettings
{
[Column("id")]
public Guid Id { get; set; }
[Column("allowed_time_ranges", TypeName = "timerange[]")]
public NpgsqlRange<TimeSpan>[]? AllowedTimeRanges { get; set; }
}
private readonly NpgsqlDataSource _dataSource;
private readonly ILoggerFactory _loggerFactory;
public class CustomDbContext : DbContext
{
private readonly NpgsqlDataSource _dataSource;
private readonly ILoggerFactory _loggerFactory;
public CustomDbContext (NpgsqlDataSource dataSource, ILoggerFactory loggerFactory)
{
_dataSource = dataSource;
_loggerFactory = loggerFactory;
}
public DbSet<RestrictionSettings> RestrictionSettings{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder
.UseLoggerFactory(_loggerFactory)
.UseNpgsql(
_dataSource,
o => o
.SetPostgresVersion(new Version(10, 21))
.MapRange<TimeSpan>("timerange", subtypeName: "time without time zone"));
optionsBuilder
.EnableDetailedErrors()
.EnableSensitiveDataLogging();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasPostgresRange(name: "timerange", subtype: "time without time zone");
}
}
Registration example
services.AddSingleton<Func<CustomDbContext>>(p => () =>
{
var connectionString = "connection string";
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.EnableDynamicJson().EnableUnmappedTypes();
var dataSource = dataSourceBuilder.Build();
return new CustomDbContext (
dataSource,
p.GetRequiredService<ILoggerFactory>());
});
The timerange it's a custom timerange type:
` CREATE FUNCTION time_subtype_diff(x time, y time) RETURNS float8 AS
'SELECT EXTRACT(EPOCH FROM (x - y))' LANGUAGE sql STRICT IMMUTABLE;
CREATE TYPE timerange AS RANGE (
subtype = time without time zone,
subtype_diff = time_subtype_diff
);`
How can we solve this error? Before update it was working,
Exception message:
`Microsoft.EntityFrameworkCore.DbUpdateException
HResult=0x80131500
Message=An error occurred while saving the entity changes. See the inner exception for details.
Source=Microsoft.EntityFrameworkCore.Relational
StackTrace:
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__50.MoveNext()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__111.MoveNext()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__115.MoveNext()
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__63.MoveNext()
at
This exception was originally thrown at this call stack:
[External Code]
Inner Exception 1:
InvalidCastException: Writing values of 'NpgsqlTypes.NpgsqlRange`1[[System.TimeSpan, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][]' is not supported for parameters having NpgsqlDbType '-2147483608'.
`
The text was updated successfully, but these errors were encountered:
which suggests that's already happening. Not sure how we end up with a is not supported for parameters having NpgsqlDbType kind of error. That only happens if it was set somehow.
I'm sorry. The issue is actually related to array of ranges NpgSqlRange<TimeSpan>[]. So the entity class should look like this.
public class RestrictionSettings
{
[Column("id")]
public Guid Id { get; set; }
[Column("allowed_time_ranges", TypeName = "timerange[]")]
public NpgsqlRange<TimeSpan>[]? AllowedTimeRanges { get; set; }
}
Usual NpgSqlRange<TimeSpan> works fine.
public class RestrictionSettings
{
[Column("id")]
public Guid Id { get; set; }
[Column("allowed_time_ranges", TypeName = "timerange")]
public NpgsqlRange<TimeSpan> AllowedTimeRanges { get; set; }
}
Is it possible to use array(NpgsqlRange[]) now in 8.02 version?
Btw, NpgsqlRange<DateTime>[] works well without specifying custom range type. But for the NpgsqlRange<TimeSpan>[] it's not
Steps to reproduce
Hi, after Npgsql.EntityFrameworkCore.PostgreSQL update from to 7.0.4 to 8.0.2. We have started to receive an exception when saving into db.
InvalidCastException: Writing values of 'NpgsqlTypes.NpgsqlRange`1[[System.TimeSpan, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][]' is not supported for parameters having NpgsqlDbType '-2147483608'.
Registration example
The timerange it's a custom timerange type:
How can we solve this error? Before update it was working,
The text was updated successfully, but these errors were encountered: