0

I am using MVC, NHibernate and Sharp architecture for my project.
Whenever I am saving objects(entities) using SaveOrUpdate there are child entities which are updated as well. But instead of updating child entities it runs DELETE-INSERT for all the child entities.

any help.

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
Shabbir Ahmad
  • 35
  • 1
  • 9
  • How are you modifying the child entities? – Cole W Oct 18 '11 at 18:53
  • That sort of behavior occurs when you modify the collection property on the entity, rather than modify/add items into the collection. NHibernate sees the change occurring to the property itself and does a delete/insert. If your loading the child items and assigning them to the entity, don't. Eager load them or lazy load them. – Phill Oct 18 '11 at 21:27
  • Please can you elaborate the question. It would be very difficult for others to help with the limited description – frictionlesspulley Oct 18 '11 at 22:38

1 Answers1

0

It happens because you are not updating existing child objects, you replace whole collection of them. That makes Nhibernate think that it has to save new collection.

Sly
  • 15,046
  • 12
  • 60
  • 89
  • Yes. this is exactly what is happening. When there is no data in child entity database, It just makes one insert in the database. But when there is already data available for child entity, it deletes the previous rows and then insert the current data all over again. – Shabbir Ahmad Oct 19 '11 at 08:50
  • Any suggestion on how this can be modified that, when ever we add a new instance of child entity, it should only add new row instead of deleting previous ones and inserting again. – Shabbir Ahmad Oct 19 '11 at 08:52
  • It's because your assigning the collection. Don't assign the collection, add your item to the existing collection. – Phill Oct 19 '11 at 12:50