-
Notifications
You must be signed in to change notification settings - Fork 600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to cast object of type 'System.Int64' to type 'System.Double'. #661
Comments
Problem association code else if (Type.IsValueType || Type == typeof(string) || Type == typeof(byte[]))
{
// Do we need to install a converter?
var srcType = reader.GetFieldType(0);
var converter = GetConverter(mapper, null, srcType, Type);
// "if (!rdr.IsDBNull(i))"
il.Emit(OpCodes.Ldarg_0); // rdr
il.Emit(OpCodes.Ldc_I4_0); // rdr,0
il.Emit(OpCodes.Callvirt, fnIsDBNull); // bool
var lblCont = il.DefineLabel();
il.Emit(OpCodes.Brfalse_S, lblCont);
il.Emit(OpCodes.Ldnull); // null
var lblFin = il.DefineLabel();
il.Emit(OpCodes.Br_S, lblFin);
il.MarkLabel(lblCont);
// Setup stack for call to converter
AddConverterToStack(il, converter);
il.Emit(OpCodes.Ldarg_0); // rdr
il.Emit(OpCodes.Ldc_I4_0); // rdr,0
il.Emit(OpCodes.Callvirt, fnGetValue); // value
// Call the converter
if (converter != null)
il.Emit(OpCodes.Callvirt, fnInvoke);
il.MarkLabel(lblFin);
il.Emit(OpCodes.Unbox_Any, Type); // value converted |
Can you please provide some code that shows your table structure, the POCO you're using, and how you're retrieving values? The following code works fine for me. CREATE TABLE IF NOT EXISTS Foo (Bar REAL)
INSERT INTO Foo VALUES (1), (1.1) public class Foo
{
public double Bar { get; set; }
}
db.Fetch<Foo>(); |
static void Main(string[] args)
{
var sql = @"CREATE TABLE IF NOT EXISTS Foo (Bar number);
INSERT INTO Foo VALUES (0.99),(1), (1.1)";
File.Create("1.db").Close();
string connectionString = "DataSource=1.db;";
var db = new Database(connectionString, new SQLiteDatabaseProvider());
db.Execute(sql);
var db2 = db.Query<double>("select Bar from Foo").ToList();
} |
That code runs just fine for me, no exceptions. What version of System.DataSQLite are you using? I'm using System.Data.SQLite.Core v1.0.117. |
TargetFramework :net7.0 |
Okay, thanks. I can reproduce with that driver; I'll look into it. |
Here's what's going on. When you run your select query, PP peeks at the first record in the DataReader and sets up a factory method that can convert the fields in the DataReader to the desired POCO (which, in this case, is just Using System.Data.SQLite.Core, the values come across as Using the Microsoft driver, however, the values come across as different types -- Try changing your code so that the In short, PP assumes that the values in a column are all of the same type -- which I think is a reasonable assumption, since they're the same type on the database side. In some ways, this is an even more specialized case of the issue discussed here, in which a column returned by a stored proc might legitimately be of different types between invocations (but always a single type for a given invocation). I would say this is a bug in the MS driver -- or at best a questionable design decision. Your best option is probably to switch to the SQLite driver. You might also be able to work around things by using a custom mapper, descending from |
I'm going to change the NUMBER type to REAL type. |
When querying with sqlite, the returned number type is double for 1.1 and int64 for 1。
Unable to cast object of type 'System.Int64' to type 'System.Double'.
The test data are as follows:
The text was updated successfully, but these errors were encountered: