I have a generic methode for load the entity. I need to check result value for null value.
public TEntity LoadById(long id)
{
TEntity result = SessionInstance.Load<TEntity>(id);
if (result != null) //This condition is always true
if (result.Id == 0 ) //Throws ObjectNotFoundException
throw new Exception("Ex Text");
return result;
}
But my condition ( if (result != null) ) is always true and next line result.Propagate() is throw ObjectNotFoundException exception by this message : No row with the given identifier exists[RCISP.Domain.Entities.Person#1000]
Because result entity is a proxy.
How can I check a condition for null value in a proxy?
Use NHibernate’s
ISession.Getinstead ofISession.Load.Loadthrows an exception if the requested item does not exist, but it might also return a proxy that is later used to load the object from the database – and will only then throw if the item does not exist. That’s what is happening to you.Geton the other side returnsnullif the item does not exist in database. Exactly what you want.More on that topic here.