by Ricardo Peres
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Ricardo Peres
Published on Mon, 19 May 2014 20:59:51 GMT Indexed on 2014/05/26 21:27 UTC
Read the original article Hit count: 142
This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
When saving a new entity that has references to other entities (one to one, many to one), one has two options for setting their values:
- Load each of these references by calling ISession.Get and passing the foreign key;
- Load a proxy instead, by calling ISession.Load with the foreign key.
So, what is the difference? Well, ISession.Get goes to the database and tries to retrieve the record with the given key, returning null if no record is found. ISession.Load, on the other hand, just returns a proxy to that record, without going to the database. This turns out to be a better option, because we really don’t need to retrieve the record – and all of its non-lazy properties and collections -, we just need its key.
So, if you just need to set a foreign key, use ISession.Load instead of ISession.Get.
© ASP.net Weblogs or respective owner