Repository navigation
Description
First of all, I've already looked into these issues:
And while they seem at first similar to my problem, the fix suggested on issue #438 does not work in my case, because as you'll see, the number at the root of this problem is an integer, thus it can't be rounded.
NgpSQL: v3.2.7
Postgres: v9.2.24
.NET framework: 4.6.1
In my particular case, I'm not using Entity Framework, I'm calling a DB function to read its data with a NpgSQLDataReader reader.
I've also tried to run the same method with NpgSQL v4.1.5 (the latest stable version available at NuGet). Obviously, I had to apply some tweaks in my code to make it run, nonetheless, this method throws the same error on v4.1.5.
In the code snippet below, the error occurs at meas.NumericValue = base.ParseDouble(reader["numeric_value"]);, when the code tries to read the value 99100000000000000000000000000000000000 from the DB, which is far higher than decimal.MaxValue.
The column numeric_value, in the DB, has the numeric type (no precision defined).
public List<Measurement> LoadMeasurementsByActivities(long[] activityKeys, ref List<long> activitiesKeyErrored)
{
List<Measurement> measurements = new List<Measurement>();
IDataReader reader = null;
try
{
this.command = conn.CreateCommand();
this.command.CommandText = "my_function_name";
this.command.CommandType = CommandType.StoredProcedure;
this.command.Connection = conn;
var activityArrayParam = new NpgsqlParameter();
activityArrayParam.ParameterName = "_activity_key_array";
activityArrayParam.Value = activityKeys;
command.Parameters.Add(activityArrayParam);
reader = base.Command.ExecuteReader();
while (reader.Read())
{
var meas = new Measurement();
...
meas.NumericValue = base.ParseDouble(reader["numeric_value"]);
meas.TestCount = base.ParseInt(reader["test_count"]);
...
measurements.Add(meas);
}
}
catch (Exception ex)
{
...
}
finally
{
...
}
return measurements;
}Stack trace:
at Npgsql.NpgsqlDataReader.GetValue(Int32 ordinal)
at Npgsql.NpgsqlDataReader.get_Item(String name)
I've also tried to use the method GetFieldValue<T>, but the result was the same.
reader.GetFieldValue<double?>(10)
Where 10 is the index that represents the column numeric_value returned by the DB function.
It looks like that the GetValue and GetFieldValue methods are trying to cast the value returned from the DB to decimal, as this is the default mapping for the numeric data type.
The function that I'm calling is pretty straightforward, just a simple select in a table with a few joins.
I've stored this value in the DB using the same property (NumericValue) that I'm trying to assign to in the code above, just using a different DB function, of course.
Am I doing something wrong here?
Is this a bug on NpgSQL?
Thanks for any assistance that you may provide.
Activity
roji commentedon Oct 15, 2020
Duplicate of #448
roji commentedon Oct 15, 2020
As you've written, the number you're trying to read cannot be represented with a .NET decimal. Trying to read it as a decimal may be a good idea, but calling
GetFieldValue<double>indeed doesn't help, as you've noticed - PostgreSQL is still sending a PostgreSQL decimal back to Npgsql over the wire, and Npgsql tries to decode that as a decimal, which it can't. In theory Npgsql could have a whole separate decoding subrouting from PostgreSQL decimal to .NET double, but that probably isn't worth the effort.However, you can easily get around this by asking PostgreSQL to cast your decimal to a double, in SQL:
This makes PostgreSQL send Npgsql a double, which can be directly read as a double; no decimal ever reaches the client.