0
我想這樣簡化我的問題在這裏,但基本上我試圖映射2實體,但我沒有在數據庫集中的外鍵,因爲列可能爲空。當我嘗試做父插入,我收到以下錯誤:流利的NHibernate關係沒有外鍵
object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave.
這是我到目前爲止有:
我的實體
public class DocumentDraft
{
public virtual Guid Id { get; set; }
public virtual string Subject { get; set; }
public virtual string ReferenceNo { get; set;}
public virtual DocumentType DocumentType { get; set; }
}
public class DocumentType
{
public virtual short Id { get; set; }
public virtual string Description { get; set; }
}
製圖
public class DocumentDraftMap : ClassMap<DocumentDraft>
{
public DocumentDraft()
{
// other mappings ...
References(x => x.DocumentType)
.Columns("DocumentTypeId")
.Nullable()
.Not.LazyLoad()
.NotFound.Ignore(); // <-- added this since the value could be null and it throws an error
}
}
我試圖在映射中指定Cascade.None()
,但我得到了相同的結果。基本上會發生的是null
值試圖插入DocumentType
,我不希望這(我想在父表中插入null,但我不想觸摸子表,我不希望這個級聯)。
我也試過:.Not.Insert()
,但那也沒用。
如果有人能幫我解決這個問題,我會很感激。
您確定保存時屬性DocumentType爲null嗎?似乎有一個實例,沒有cascade.All()引用它不能被保存。 – Firo
@Firo保存時爲空,這就是問題所在,我希望它在數據庫中保存爲空,我一整天都在拉我的頭髮。有時候NHibernate會非常煩人,我得告訴你。 –
@Firo另外我已經編輯了一下我的問題,因爲它有點誤導。我想更新父表而不是孩子(不想讓它級聯)。 –