The objects
We have an object called Product that loads using NHibernate.
The product has a category that NHibernate happily fills for me.
Database
In the database, the product has a foreign key for the category.
Scenario
The user edits this product (via the web interface) and selects a different category (instead of "Fish" we select "Veg").
This is probably a drop-down list showing each category. When they select another category, we get an int key.
Problem
Obviously, now we want to save the changes to Product, but in fact the only change is to save the new int (say 2 instead of 1).
So, we retrieve the existing Product, and now the problem arises.
We do not have the "CategoryID" field in Product, we only have the Category property.
But we really do not want to retrieve a category (by id) just to assign it to the Product.
So, I think I want to know if we should ...
a) Add the CategoryID property to Product
b) Create a new category, assign an appropriate identifier to it and attach it to the Product (but it will probably cause errors or overwrite the existing category)
c) Get (find) a category from the system (by id) and attach it to the product
d) Do something else!
source share