简体   繁体   English

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

[英]Fluent NHibernate - Set reference key columns to null

I have a table of Appointments and a table of AppointmentOutcomes. 我有一份预约表和一份AppointmentOutcomes表。 On my Appointments table I have an OutcomeID field which has a foreign key to AppointmentOutcomes. 在我的Appointments表上,我有一个OutcomeID字段,它有一个AppointmentOutcomes的外键。 My Fluent NHibernate mappings look as follows; 我的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);

Using NHibernate, if I delete an AppointmentOutcome an exception is thrown because the foreign key is invalid. 使用NHibernate,如果我删除了AppointmentOutcome,则抛出异常,因为外键无效。 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. 我想要发生的是删除AppointmentOutcome会自动将引用AppointmentOutcome的任何约会的OutcomeID设置为NULL。

Is this possible using Fluent NHibernate? 这是否可以使用Fluent NHibernate?

You need to set the Outcome reference to null on the Appointment object when you delete an Outcome. 删除结果时,需要在约会对象上将结果引用设置为null。

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. 如果结果可以链接到多个约会,那么在删除结果之前,您需要在所有这些约会上取消引用结果(或设置级联删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM