开发者

Many-To-One relationships in NHibernate

开发者 https://www.devze.com 2022-12-11 21:37 出处:网络
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 lo

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开发者_JAVA技巧. 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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消