Nhibernate Flush causing Updates where there should be none

前端 未结 2 1487
萌比男神i
萌比男神i 2021-01-25 00:31

I am using the Repo pattern, and I have set up tests to replicate my a HTTP request coming in and then causing dispose on a unit of work once a test has completed.

It ap

相关标签:
2条回答
  • 2021-01-25 00:52

    I faced a similar problem. I'll first tell you what causes this. When NHibernate fetches an entity from the DB it assigns values to it's props. There are few props that have null values in the DB but are not of Nullable type in the class definition. So NHibernate assigns them a default value eg. 0 for int, DateTime.MinValue for datetime etc. When you call commit on the transaction, NHibernate rechecks the values of properties with DB values and since props which should have had Null values now have a default value, NHibernate thinks that the values have been changed and causes an update.

    Solution:

    1. Use nullable datatypes for your class props by post fixing them with ? but for me this is causing other problems.
    2. Map your properties as Not Null Types but this is not preferable in most cases.
    3. The solution that I am using: I am assigning default values to the props in the entity's constructor, so instead of saving Null values in the Db Nhibernate saves some default value and this stops the calls to unnecessary updates.

    You may further google NHibernate ghostbuster for more research on this problem.

    0 讨论(0)
  • 2021-01-25 01:00

    NHibernate typically runs updates when it has transient or detached entities that it isn't sure about. That is, entities that it doesn't know if it has a parent for that manages it or if its not sure the entity is dirty. This is typically a symptom of a bad mapping somewhere (a missing Inverse on some parent) or you have no Version or Date column on your entities.

    0 讨论(0)
提交回复
热议问题