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)" />后,问题解决。
绕了一大圈才意识到,这是在定义数据类型。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)