You are here: Home / Blog / posts / Programming Magic / Force refresh of data from database with NHibernate

Force refresh of data from database with NHibernate

by admin published Jan 28, 2014 02:11 PM, last modified Jan 28, 2014 02:11 PM
Avoid the black magic of NHibernate thinking that it does not need to re-get the data from the database when doing unit tests.

Database items that automatically get populated like lists of other database records linked with a bag in the mapping file may not be populated if the GetById doesn't think that data has been changed.

For example, if a ceremony has related registrations, and the ceremony is added to those registrations, then the IList will probably not be populated in the ceremony.

You may think, I'll just do a get from the database and it will refresh. But no, NHibernate may think that you have an existing copy of the ceremony in the database, so it will avoid the call to the database, and your data will not be refreshed. To get around this, you want to evict the ceremony entity before getting it, this will force NHibernate to make the call to the database and the list will be populated.

NHibernateSessionManager.Instance.GetSession().Evict(entity);

 

When:

Where:

Contact