I have a NHibernate entity with the following field:
public virtual decimal ApprovedQuantity { get; set; }
and the ClassMap for this entity contains the following:
Map (x => x.ApprovedQuantity).Column("approved_qty").CustomType<decimal>().Precision(7).Scale(2);
The SQL Server table is defined as follows:
CREATE TABLE stock (
project_code NCHAR(30) NOT NULL,
item_code NCHAR(25) NOT NULL,
classification NCHAR(100) NOT NULL,
name NCHAR(30) NOT NULL,
depot_reference NCHAR(12) NOT NULL,
total_qty DECIMAL(7, 2) NOT NULL,
approved_qty DECIMAL(7, 2) NOT NULL,
unapproved_qty DECIMAL(7, 2) NOT NULL,
last_modified SMALLDATETIME,
PRIMARY KEY (project_code, item_code, classification, name, depot_reference)
When I build a NHibernate criteria to run a query against this table, to get all stock items with an approved quantity > 200 (grouped by various other fields), the generated SQL as logged by NHibernate looks like the following:
NHibernate: SELECT this_.item_code as y0_, this_.name as y1_, this_.classification as y2_, sum(this_.total_qty) as y3_, sum(this_.approved_qty) as y4_, sum(this_.unapproved_qty) as y5_ FROM stock this_ WHERE this_.project_code = @p0 and ((this_.depot_reference = @p1 or this_.depot_reference = @p2 or this_.depot_reference = @p3)) GROUP BY this_.item_code, this_.name, this_.classification HAVING sum(this_.approved_qty) > @p4 ORDER BY y0_ asc;@p0 = 'MyProject' [Type: AnsiStringFixedLength (8000)], @p1 = '10R' [Type: AnsiStringFixedLength (8000)], @p2 = '16Z' [Type: AnsiStringFixedLength (8000)], @p3 = '17T' [Type: AnsiStringFixedLength (8000)], @p4 = 200 [Type: Currency (8)]
Note that at the very end of this log entry, the @p4 parameter is declared as type 'currency' by NHibernate, despite me explicitly mapping the field in the ClassMap to the 'decimal' type.
I wouldn't have thought that this in itself would cause too much of a problem, but on the SQL Server end, it ends up compiling the @p4 parameter into a value of '0.02'. I've obtained this by gaining the XML of the query plan at the SQL Server end:
<ColumnReference Column="@p4" ParameterCompiledValue="($0.0200)"
So SQL Server basically ends up treating my value of '200' as '0.02', and of course the query returns unexpected results.
If I can get NHibernate to declare @p4 as a decimal type, and not as a currency type, I'm hoping this problem will be resolved. Why does it appear to be ignoring my attempt at explicitly mapping this field to the 'decimal' SQL Server type in the ClassMap? Even without the explicit mapping to CustomType, it still uses the 'money' SQL Server type.
Thanks.