Fluent NHibernate - 将引用键列设置为 null

发布于 2024-08-29 12:36:46 字数 698 浏览 2 评论 0原文

我有一个约会表和一个约会结果表。在我的 Appointments 表上,我有一个 OutcomeID 字段,其中包含 AppointmentOutcomes 的外键。我的 Fluent NHibernate 映射如下所示;

        Table("Appointments");
        Not.LazyLoad();
        Id(c => c.ID).GeneratedBy.Assigned();
        Map(c => c.Subject);
        Map(c => c.StartTime);
        References(c => c.Outcome, "OutcomeID");


        Table("AppointmentOutcomes");
        Not.LazyLoad();
        Id(c => c.ID).GeneratedBy.Assigned();
        Map(c => c.Description);

使用 NHibernate,如果我删除 AppointmentOutcome,则会抛出异常,因为外键无效。我希望发生的是,删除 AppointmentOutcome 会自动将引用 AppointmentOutcome 的任何约会的 OutcomeID 设置为 NULL。

使用 Fluent NHibernate 可以实现这一点吗?

I have a table of Appointments and a table of AppointmentOutcomes. On my Appointments table I have an OutcomeID field which has a foreign key to AppointmentOutcomes. My Fluent NHibernate mappings look as follows;

        Table("Appointments");
        Not.LazyLoad();
        Id(c => c.ID).GeneratedBy.Assigned();
        Map(c => c.Subject);
        Map(c => c.StartTime);
        References(c => c.Outcome, "OutcomeID");


        Table("AppointmentOutcomes");
        Not.LazyLoad();
        Id(c => c.ID).GeneratedBy.Assigned();
        Map(c => c.Description);

Using NHibernate, if I delete an AppointmentOutcome an exception is thrown because the foreign key is invalid. What I would like to happen is that deleting an AppointmentOutcome would automatically set the OutcomeID of any Appointments that reference the AppointmentOutcome to NULL.

Is this possible using Fluent NHibernate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2024-09-05 12:36:46

删除 Outcome 时,需要将 Appointment 对象上的 Outcome 引用设置为 null。

using (var txn = session.BeginTransaction())
{
    myAppointment.Outcome = null;
    session.Delete(outcome);
    txn.Commit();
}

您将关系映射为一对多结果到约会(一个结果可以链接到多个约会)。如果一个结果可以链接到多个约会,那么您需要在删除结果之前取消对所有约会的结果引用(或设置级联删除)。

You need to set the Outcome reference to null on the Appointment object when you delete an Outcome.

using (var txn = session.BeginTransaction())
{
    myAppointment.Outcome = null;
    session.Delete(outcome);
    txn.Commit();
}

You have the relationship mapped as one-to-many Outcome-to-Appointment(one outcome can be linked to multiple appointments). If an Outcome can be linked to multiple appointments then you would need to de-reference Outcome on all of them (or set cascading delete) before deleting the Outcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文