开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜