An optimization for SQL Server query plan cache set decimal parameters precision and scale to their "max" values in SqlClientDriver if they do not specify those themselves. And max scale is only 5. Any way, max scale is non-sens, since it is indeed equal to precision, in which case their are no more digits before the dot. It is more a default scale. And such a default scale is quite a nasty thing. It causes the value to be truncated (and not rounded) to 5 digits after the dot, when the query parsing was not able to infer its precision/scale from compared entity property (due to the expression). This issue has been isolated in NH-4087.
An undue cast to the NHibernate default decimal (decimal(19, 5)) is done on the expression, causing a round. So this undue cast may compensate the first bug when truncation and rounding are equivalent... But fixing the first bug will leave us with the second anyway.
My table has two decimal values with precision 15 and scale 9.
In my query i want to select which value to select based on a boolean (UsePreviousRate).
If PreviousRate is 12345.123456789 i expect the property Rate in RateDto to be the same but the value is rounded to 5 decimals (12345.12346)
Generated SQL looks like
The same query with queryover and conditional projection returns expected value
Generated sql