4

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

5
  • does it occure if you use decimal instead of nullable<decimal>?
    – Firo
    Apr 26, 2012 at 9:51
  • @Firo - Same issue with non-nullable decimal.
    – SkipHarris
    Apr 26, 2012 at 14:19
  • 1
    then the only way to see the difference is to output what FNH generates new PersistanceModel(); model.Add(new Class1Map()); model.Write(Console.Out);
    – Firo
    Apr 26, 2012 at 14:40
  • @Firo - Tried your suggestion and used the XML that FNH generated. It looked very similar to the mapping that I did manually. When I use the FNH generated XML everything works fine. So still not sure why FNH mapping does not work.
    – SkipHarris
    Apr 26, 2012 at 15:35
  • FNH just feeds NH the xml you see directly, theree should be no difference between using FNH or FNH generated xml.
    – Firo
    Apr 27, 2012 at 5:55

1 Answer 1

3

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:

    public void Apply(IPropertyInstance instance)
    {
        instance.Length(350);
    }

But my acceptance criteria did not exclude decimal fields. I added in some acceptance criteria to exclude decimals:

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria
            .Expect(x => x.Type != typeof(decimal) && x.Type != typeof(decimal?));
    }

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.