Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't Session.Load<User>(id) return null if nothing was found?

Tags:

nhibernate

When I try and load an entity by ID using:

Session.Load<User>(21);

I get a 'no row with the given identifier exists'.

In my code I was checking for null like:

if(user == null)

How am I suppose to know if the row didn't exist, or how can I make it return null instead?

like image 859
mrblah Avatar asked Jan 22 '23 20:01

mrblah


1 Answers

Because it doesn't actually make a round-trip to the database. You are actually getting back a proxy for lazy loading so NHibernate can't know if it really exists or not. If you need to know if really exists, you should use:

var entity = Session.Get(21)
like image 109
Mike Valenty Avatar answered Mar 03 '23 10:03

Mike Valenty