c# - Nhibernate:删除孤儿,但在删除父项时不删除子项

标签 c# nhibernate

我正在寻找一种配置 nhibernate 的方法,以便自动删除孤立的子实体,但如果父实体被删除而子实体存在,则会对父实体执行删除查询,但不会对子实体执行删除查询。基本上我想将我的级联选项设置为“save-update-orphan”,但这不受支持。

    <set name="children" inverse="true" cascade="all-delete-orphan" access="field">
        <key column="ParentId" />
        <one-to-many class="Parent" />
    </set>

all-delete-orphan 对我不起作用,因为它会在删除父项时自动删除子项。

更新 尝试更清楚......当我从 parent 的集合中明确删除 child 时,我希望删除 child 。当我明确删除父项时,我不想删除子项。

最佳答案

我不相信你可以通过配置做你想做的事。

我能想到的唯一选择是关闭孤立删除,并在需要删除子项时手动删除它。

NHibernate Cascades: the different between all, all-delete-orphans and save-update

Here is what each cascade option means:

  • none - do not do any cascades, let the users handles them by themselves.
  • save-update - when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).
  • delete - when the object is deleted, delete all the objects in the assoication.
  • delete-orphan - when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.
  • all - when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
  • all-delete-orphan - when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.

关于c# - Nhibernate:删除孤儿,但在删除父项时不删除子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8612730/

相关文章:

c# - 调试 Nuget 包时找不到符号源代码

c# - ASP.NET MVC 是否适合高度安全的面向公众的站点?

c# - 如何在 .NET 应用程序中松耦合数据库列?

c# - 为什么我的 NHibernate 查询这么慢?

nhibernate - HiLo vs 身份?

c# - 可以在 C# 中使用泛型表单吗?

c# - 全局数据真的那么糟糕吗?

unit-testing - 使用 MbUnit3 的 [Rollback] 进行单元测试 NHibernate 与 SQLite 的交互

sql-server - NHibernate.Exceptions.GenericADOException 代码超时,但数据库超时

c# - NHibernate - 在删除时将引用设置为空