We have create screens with duplicate option for each object. For ex: While creating new Customer along with details, user can chose existing customer to copy groups associated from existing to new user. So I would like to know how to assign properties of new customer for 1:n and m:n scenario.
-
For “Customer” and “CustomerGroups”. Will the below approach work fine?
Customer existing = repo<Customer>(id); Customer newCust = new Customer(); for(var group in existing.Groups) newCust.CustomerGroups.Add(new CustomerGroup(){ **AllpropertiesexceptID**, **Customer=newCust** } ); -
For Order and OrderItems, since its m:n relationship, just attaching existing items with new order.
Orders existing = repo(id);
Order newOrder = new Order();
for(var item in existing.Items)
newOrder.Items.Add(item);
Is it required to do Session.Evict for the existing Order or Customer for performing these operations.
You might be thinking too much. 🙂 Just go ahead and create separate lookalike objects, pretending NHibernate isn’t even there. As long as the new instances have different (unset) identity properties, NHibernate won’t even realize they are “copies”.