tags:

views:

43

answers:

2

When I try and load an entity by ID using:

Session.Load(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?

+4  A: 

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)
Michael Valenty
I am doing that: return Session.Load<T>(id); oops, ok just read you wrote GET. thanks!
mrblah
+1  A: 

Use Get<T>(id) (more detailed explanation about NH get/load/query)

Pasi Savolainen