views:

79

answers:

2

Hi,

We want to use NHibernate behind our WCF service but we are having problems persisting child record deletes to the database.

We use DTO's between the service and client which have IList<> as the child collection type. When we populate a DTO using NHibernate it sets the child collection to be a type of Bag. However, when this is sent over the wire WCF will change this collection type to an Array.

If we delete a record from the array and pass it back to the service, NHibernate will not persist the deletion to the database.

Is there somehow we can tell NHibernate (in the mapping?) to delete any child records that do not exist in the Array?

Thanks in advance Matt

+2  A: 

There is a function on the session object called SaveOrUpdateCopy which is specifically usefull in this scenario.

SaveOrUpdateCopy (or Merge which does the same thing) will load the current entity from the database and try to detect what changes has been done (ie. what child entities in collections has been removed etc).

Here is a link to a blogpost that goes into more detail about SaveOrUpdate copy: http://www.codinginstinct.com/2009/11/nhibernate-feature-saveorupdatecopy.html

Torkel
Yes, this does the job perfectly - thanks!
Matt
A: 

Have you looked at setting cascade on the relation (i.e. list)? For some more detail on the various options, check this post.

Fried Hoeben