Reputation: 14743
I am still trying to learn NHibernate best practices, so please be easy on me... :-)
I have a Product class with a Many-To-One relationship set up to a Category class. On my Add A Product page, I am loading a DropDownList with the Categories. Now when the user enters the product information and clicks submit, I have to pull down the Category by ID (DropDownList SelectedValue) from the database to populate the Category property of my Product object so I can save it.
It seems like the Category by ID lookup is a waste. Is there a way I can simply use the ID value that was retrieved from the DropDownList?
Thanks for any advice!
Upvotes: 1
Views: 213
Reputation: 32585
Use ISession.Load() to create a proxy object. eg:
myProduct.Category = session.Load<Category>(userSuppliedCategoryId);
Note that the Load() method doesn't actually hit the database unless you try accessing a property besides the primary key, so there is no extra DB hit.
Ayende has a good post about this
Upvotes: 2