views:

16

answers:

1

I have an entity, TrackLog that has a component collection of TrackPoints. I create a new TrackLog and add some Trackpoints to it and save it to the DB.

When I grab my TrackLog again and add a new TrackPoint, instead of doing one SQL INSERT like I expect, it's doing a DELETE on all the TrackPoints associated with the TrackLog and then re-adding every TrackPoint that's in the collection.

This is the output from NHProf:

UPDATE TrackLog SET Version = 2 etc...
DELETE FROM TrackPoints WHERE TrackLog_Id = '80cc1e0d' etc...
INSERT INTO TrackPoints etc..
INSERT INTO TrackPoints etc...
INSERT INTO TrackPoints etc...

My question is, is this normal behavior for a component collection? This happens as well for my enum collections. If this is not normal behavior, how do I modify my mappings so that it exhibits the correct behavior (only one INSERT for the TrackPoint I added)?

+1  A: 

It is normal.

To avoid it, declare your collection as a set instead of a bag (and use compatible types) if your use case supports it (i.e. there can't be two trackpoints with the same data)

Diego Mijelshon
Thanks, I just came to the same conclusion myself. Another thing to keep in mind is that your components must override `Equals()` and `GetHashCode()`, or else performance is even worse (rather than one DELETE statement, it generates a DELETE for each item, then does an INSERT for each again).
Daniel T.
Yes, that's correct and it makes sense because components don't have an identifier, so that's the only way (for NH) to tell them apart.
Diego Mijelshon