由于计算属性的小数精度而导致虚拟更新

发布于 2024-08-27 10:56:24 字数 549 浏览 8 评论 0原文

本文描述了我的问题。我有几个已计算的属性。这些在 SQL Server 中键入为decimal(9,2),在我的 C# 类中键入为decimal。问题的一个例子是:

  1. 加载对象时属性值为 14.9
  2. 执行计算后属性值更改为 14.90393
  3. 当刷新会话时,NHibernate 发出更新,因为属性是脏的
  4. 由于数据库字段是十进制的 ( 9,2) 存储的值不会改变

基本上每次加载该对象时都会发出虚拟更新。我不想截断业务对象中的计算,因为这将它们与数据库紧密耦合,并且我不想丢失其他计算中的精度。我尝试在映射文件中设置比例和精度或 CustomType("Decimal(9,2)"),但这似乎只影响模式生成。

我唯一合理的选择似乎是创建一个 IUserType 实现来处理这个问题。有更好的解决方案吗?

This article describes my problem. I have several properties that are calculated. These are typed as decimal(9,2) in SQL Server and decimal in my C# classes. An example of the problem is:

  1. Object is loaded with a property value of 14.9
  2. A calculation is performed and the property value is changed to 14.90393
  3. When the session is flushed, NHibernate issues an update because the property is dirty
  4. Since the database field is decimal (9,2) the stored value doesn't change

Basically a phantom update is issued every time this object is loaded. I don't want to truncate the calculations in my business objects because that tightly couples them to the database and I don't want to lose the precision in other calculations. I tried setting scale and precision or CustomType("Decimal(9,2)") in the mapping file but this appears to only affect schema generation.

My only reasonable option appears to be creating an IUserType implementation to handle this. Is there a better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-09-03 10:56:24

改为映射到舍入属性

Map to a rounded property instead

巴黎盛开的樱花 2024-09-03 10:56:24

在我看来,这更像是一个域问题,而不是 NHibernate 问题。

您应该将小数字段复制到 DTO 对象以执行所有计算,然后与原始 NH 域实体一起传回模型/存储库方法。此时,您可以应用舍入来满足您的精度范围,然后将舍入后的数字与原始 NH 实体进行比较,并根据需要进行更新和保存。

This seems to me to be more of a domain issue than an NHibernate issue.

You should copy your decimal field(s) to a DTO object to do all of your calculations and then pass back into your model/repository method along with the original NH domain entity. At this point you can apply rounding to meet your precision scale and then compare the rounded number to the original NH entity and update and save as necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文