开发者

Can NHibernate be configured fluently to delete children when their references are set to null?

开发者 https://www.devze.com 2023-01-04 13:58 出处:网络
I\'ve heard this can also be accomplished with开发者_如何学Python 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 orpha

I've heard this can also be accomplished with开发者_如何学Python 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!


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();
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号