views:

28

answers:

2

I've heard this can also be accomplished with triggers, but I'd rather not go that road if I can. Right now it seems like nulling references to child objects just leaves them orphaned in the database, which isn't ideal to say the least.

Thanks!

A: 

I don't have Fluent.NH here but I know you can specify the cascade type for a mapping. Setting it to all-delete-orphan should do what you're asking.

If you're using convention based configuration this should give you a starting point..

http://stackoverflow.com/questions/586888/cascade-saves-with-fluent-nhibernate-automapping

ShaneC
+3  A: 

You can set the cascade option to delete orphans:

HasMany(x => x.Children).KeyColumn("ParentId").AsBag().Inverse()
    .Cascade.AllDeleteOrphan();

To make this work you need to remove the child object from the parent's collection and flush the session:

using (var txn = session.BeginTransaction())
{
    parent.Children.Remove(child);
    txn.Commit();
}
Jamie Ide