我有一個僱主對象,可以有多個人物:NHibernate的刪除父在很多一對一的關係引起外鍵衝突
//entities
public class Person {
public virtual Guid Id {get;set;}
public virtual string Name {get;set;}
public virtual Employer CurrentEmployer {get;set;}
}
public class Employer {
public virtual Guid Id {get;set;}
public virtual string Name {get;set}
public virtual IList<Person> Employees {get;set;}
}
//person to employer mappings
References(x => x.CurrentEmployer)
.Cascade.All()
.Column("CurrentEmployerId")
.ForeignKey("FK_Person_CurrentEmployer");
//employer to person mappings
HasMany(x=> x.Employees)
.Inverse()
.Cascade.All();
當我嘗試刪除鏈接到一個人的僱主,我獲得「外鍵違規」錯誤。
//example
_session.Delete(oldEmployer);
如何讓nHibernate在刪除僱主之前清空CurrentEmployerId列?
感謝您的想法,但我的一部分要求是,通過nhibernate執行刪除。 – wusher 2010-11-04 20:35:13
在我的兩個解決方案中,您仍然會使用nhibernate執行刪除;在第一個代碼中,你的代碼只是在刪除之前包含一條更新語句,在第二個時候,攔截器/監聽器將執行更新語句以響應查看刪除的發生。 – 2010-11-05 00:59:06