I\'ve a problem showing data from database. If I update an object, sometimes I get the old data, sometimes the new one. Update function works well (I can see in DB the right
You can open a new session in order to get "fresh"(updated from database) data without old entities from session cache. In the example below you can see as an entity is being querying from database. Also you can use the same mechanism to return the entity instead of a boolean value, or call session.Refresh() (from your current session of course) to refresh latest changes from database:
///
/// Gets an item exists on database.
///
/// Item ID to check if exists on database
/// True if the to check exists on database, otherwise false.
public bool Exists(object Id)
{
using (var session = NHibernateSessionHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
//get if the item is new. If is new, entity will be null and non exists
return session.Get(Id) == null ? false : true;
//also you can return the entire table from database, or filter:
//session.CreateCriteria().List();
}
}
}
public void Refresh(object entity)
{
//get latest changes from database (other users/sessions changes, manually, etc..)
NHibernateSessionHelper.CurrentSession.Refresh(entity);
}
I hope this helps you.