3

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!

Brandon Linton
  • 4,373
  • 5
  • 42
  • 63

2 Answers2

7

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
  • 48,427
  • 16
  • 81
  • 117
0

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..

Cascade Saves with Fluent NHibernate AutoMapping

Community
  • 1
  • 1
Shane Courtrille
  • 13,960
  • 22
  • 76
  • 113