I have two associated business objects - A and B. the association is (A->B)many-to-one, with B.Id a foreign key in A (so A has A.B_id in the DB).
I\'m using lazy=tru
If you are using NHibernate 3.2 or later, you could use the following code to get the id of associated object without another roundtrip to database to load the whole object:
using NHibernate.Proxy;
...
object id = null;
if (obj.IsProxy()) // obj is the object you want to get its identifier.
{
var proxy = obj as INHibernateProxy;
if (proxy != null)
{
var li = proxy.HibernateLazyInitializer;
if (li != null)
id = li.Identifier;
}
}