gpt4 book ai didi

nhibernate - Fluent NHibernate - 将引用键列设置为空

转载 作者:行者123 更新时间:2023-12-04 01:35:34 25 4
gpt4 key购买 nike

我有一个约会表和一个约会结果表。在我的 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 吗?

最佳答案

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

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

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

关于nhibernate - Fluent NHibernate - 将引用键列设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2675812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com