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另外我已经编辑了一下我的问题,因为它有点误导。我想更新父表而不是孩子(不想让它级联)。 –