Open fairking opened 4 years ago
If I remove that avg column from the select, the query is passed.
Seems some incorrect type is detected for your avg field. Just register it manually via AddScalar
:
.AddScalar("column3_avg", NHibernateUtil.Int32 /* or whatever type is needed */);
I am unable to reproduce this exception. The following test:
public class MyViewModel
{
public string CustomerId { get; set; }
public string ShipName { get; set; }
public int? Average { get; set; }
}
[Test]
public void Test()
{
var result = session
.CreateSQLQuery("select CustomerId, ShipName, avg(EmployeeId) as Average from Orders group by CustomerId, ShipName")
//.AddScalar("CustomerId", NHibernateUtil.String)
//.AddScalar("ShipName", NHibernateUtil.String)
//.AddScalar("Average", NHibernateUtil.Int32)
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(MyViewModel)))
.List<MyViewModel>();
}
throws:
System.ArgumentException : Object of type 'System.Double' cannot be converted to type 'System.Nullable`1[System.Int32]'.
for Average
property which is expected as avg
function produce an integral value. For some reason GetHibernateType returns AnyType
in your case when auto discovering column types. Did you register any custom types using TypeFactory.RegisterType
method?
Did you register any custom types using TypeFactory.RegisterType method?
I don't do any AddScalar
calls or any other methods to register types. Also I don't have any custom types (User Types).
If I do:
public decimal? Average { get; set; }
I got the same error.
However if I register a type AddScalar("Average", NHibernateUtil.Int32)
the error disappears.
I am going to use it as a workaround. :-)
Thanks guys
Just want to mention one more thing. It only happens with SQLite. MSSQL same query is working fine. As I use MSSQL db in production and SQLite in memory in query tests.
Nh 5.3.3 (also 5.3.2) (not sure about older versions) .NET Core 3.1
Config:
Having the following sqlite query:
Where the column3 is
nullable integer
and the column3_avg property isint?
.Gives the following error:
Any idea why is it happening guys? Is there something not implemented yet?
Thanks