NHibernate中的小数精度问题

用nhibernate更新sql server中的decimal(18,6)类型的数据时,发现不论前端输入几位小数,保存到数据库后都只能保留5位小数,用profiler跟踪发现,在它自动生成的sql语句中数据类型一律被转为decimal(19,5),所以会导致只保留5位小数。

用了两天时间,寻找各种解决方案,过程不记了。

NHihbernate.dll所在目录中,无意中发现了一个NHihbernate.xml文档,其中有关于精度的描述

        <member name="T:NHibernate.SqlCommand.ParameterPrecisionScale">
            <summary>
            An immutable Parameter that supports a Precision and a Scale
            </summary>
            <remarks>
            This should only be used when the property needs to be mapped with
            a <c>type="Decimal(20,5)"</c> because for some reason the default parameter
            generation of <c>decimal(19,5)</c> (MsSql specific) is not good enough.
            </remarks>
        </member>

项目编译后会自动生成此文档。从引用中右键NHihbernate,属性,搜索NHibernate.SqlCommand.ParameterPrecisionScale,这是一个类,它的描述如下:

摘要:
An immutable Parameter that supports a Precision and a Scale

注解:
This should only be used when the property needs to be mapped with a type="Decimal(20,4)" because for some reason the default parameter generation of decimal(19,5) (MsSql specific) is not good enough.

 

根据注解中的描述,在实体映射文件hbm.xml中的对应proerty中增加一个type="Decimal(20,4)"元素,可以解决问题。

经实验,改成<property name="FJPYRate" type="Decimal(18,6)" />后,问题解决。

 

绕了一大圈才意识到,这是在定义数据类型。

 

posted @ 2022-10-12 15:12  火军刀  阅读(106)  评论(0编辑  收藏  举报