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:
- Object is loaded with a property value of 14.9
- A calculation is performed and the property value is changed to 14.90393
- When the session is flushed, NHibernate issues an update because the property is dirty
- 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 p开发者_JAVA技巧recision 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?
Map to a rounded property instead
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.
精彩评论