I have a case where decimal fields are being truncated to five decimal places when saving to the database. This happens even though I have mapped them with a precision of 10 places after the decimal.
I’m using NHibernate 3.2.0.4000 and FluentNHibernate 1.3.0.717 with SQL Server 2008.
Here is my C# property:
public virtual decimal? MyDecimalField { get; set; }
I’ve tried doing an auto mapping override:
mapping.Map(x => x.MyDecimalField).Precision(28).Scale(10);
I’ve also tried doing explicit mapping of the field:
Map(x => x.MyDecimalField).Precision(28).Scale(10);
When doing a schema export, the table gets created correctly:
create table MyTable (
Id UNIQUEIDENTIFIER not null,
MyDecimalField DECIMAL(28, 10) null,
primary key (Id)
)
Using a value of 5.1234567890, here are snippets of parameters from the generated SQL update statement:
@p31 decimal(28,5)
@p31=5.12345
I am able to make this work correctly if I use XML mapping:
<property name="Decimal01" precision="28" scale="10"/>
I have removed all conventions for decimal fields and still have the problem.
Since XML based mapping works, this leads me to believe that it is a problem with Fluent. Any suggestions on something else I may have missed?
Thanks,
Skip
I’m going to answer my own question here. Thanks to @Firo for giving me some tips that pointed me to a solution.
I have a convention to set the default length for my string fields:
But my acceptance criteria did not exclude decimal fields. I added in some acceptance criteria to exclude decimals:
Doing this corrected my decimal precision.
What’s interesting is that my default string length of 350 did not appear in my exported XML mapping files for the decimal fields. But it appears that FNH was still using it somehow. This seems like a FNH bug to me.