![](/img/trans.png)
[英]fluent nhibernate nullable foreign key “null reference exception”
[英]Fluent NHibernate - Set reference key columns to null
我有一份預約表和一份AppointmentOutcomes表。 在我的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?
刪除結果時,需要在約會對象上將結果引用設置為null。
using (var txn = session.BeginTransaction())
{
myAppointment.Outcome = null;
session.Delete(outcome);
txn.Commit();
}
您將關系映射為一對多結果到預約(一個結果可以鏈接到多個約會)。 如果結果可以鏈接到多個約會,那么在刪除結果之前,您需要在所有這些約會上取消引用結果(或設置級聯刪除)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.