c# - Nhibernate - 与 Cascade all-delete-orphan 一对一映射,不删除孤儿

标签 c# nhibernate domain-driven-design nhibernate-mapping ddd-repositories

我有一个“面试”实体,它与“表单提交”实体具有一对一的映射关系,可以说面试实体是主导方,映射是:

<class name="Interview">
    <id name="Id" column="Id" type="Int64">
        <generator class="identity" />
    </id>

    // other props (snip)....

    <one-to-one name="Submission" class="FormSubmission"
        cascade="all-delete-orphan" />
</class>

<class name="FormSubmission">
    <id name="Id" column="Id" type="Int64">
        <generator class="foreign">
            <param name="property">Interview</param>
        </generator>
    </id>

    // other props (snip)....

    <one-to-one name="Interview" class="Interview"
        constrained="true" cascade="none" />
</class>

两个实体都是聚合的一部分,采访充当聚合根。我正在尝试通过 Interview 实体保存/更新/删除 FormSubmission,因此我将关联的 Interview 端映射为 cascade="all-delete-orphan"。例如,我可以像这样创建一个新的 FormSubmission:

myInterview.Submission = new FormSubmission(myInterview);
InterviewRepository.Save(myInterview);

...这很好用,FormSubmission 已保存。但是,我似乎无法删除我尝试这样做的 FormSubmission:

myInterview.Submission = null;
InterviewRepository.Save(myInterview);

...但这似乎并没有删除 FormSubmission。我已经尝试将 null 分配给关联的双方:

myInterview.Submission.Interview = null;
myInterview.Submission = null;
InterviewRepository.Save(myInterview);

我什至尝试在 FormSubmission 端设置 cascade="all-delete-orphan",但似乎没有任何效果。我错过了什么?

最佳答案

可能这不是您想要的答案。根据此问题,主键一对一关联不支持“全部删除孤立”级联:https://nhibernate.jira.com/browse/NH-1262 .即使外键一对一关联也很可能忽略“all-delete-orphan”级联:

<class name="Interview">
    <id name="Id" column="Id" type="Int64">
        <generator class="identity" />
    </id>

    <property name="Name" />

    <many-to-one name="Submission" unique="true" cascade="all-delete-orphan" />
</class>

<class name="FormSubmission">
    <id name="Id" column="Id" type="Int64">
        <generator class="identity" />
    </id>

    <property name="Name" />

    <one-to-one name="Interview" cascade="all-delete-orphan" property-ref="Submission"  />
</class>

编辑: 查普曼 suggests使用拦截器(事件监听器在 NH2.x 及更高版本中更受欢迎)来模拟这个听起来很有趣的功能,但我还不清楚如何实现这样的拦截器/事件监听器。

关于c# - Nhibernate - 与 Cascade all-delete-orphan 一对一映射,不删除孤儿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704984/

相关文章:

c# - 如何在 ASP.NET Core 2.0、RAZOR Tag Helpers 中格式化日期、时间、数字

c# - 在 NHibernate 3.3 中使用 Fluent NHibernate

c# - DDD 封装和存储库模式

c# - 架构 : simple CQS

c# - 添加到 CHAR 列末尾的一系列空格

c# - 是否有任何类可以像数据库一样查询/修改但完全是程序本地和内部的?

nhibernate - 无法将 NHibernate.Collection.Generic.PersistentGenericBag 类型的对象强制转换为 List

entity - 差异聚合根/实体

javascript - 为什么 textbox.disabled= true 会清除文本框的文本?

nhibernate - 通过代码进行 Nhibernate 映射的 StringType