[英]Fluent NHibernate Relation Without Foreign Key
I'm trying so simplify my problem here, but basically I'm trying to map 2 entities however i don't have a Foreign Key in the database set, since the column could be null. 我正在尝试这样简化我的问题,但基本上我正在尝试映射2个实体但是我没有数据库集中的外键,因为列可能为null。 When I try to do an insert on the parent, I'm getting the following error:
当我尝试在父项上插入时,我收到以下错误:
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.
object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例,或者将属性的级联操作设置为使其自动保存的内容。
This is what I have so far: 这是我到目前为止:
My entities 我的实体
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; }
}
Mapping 制图
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
}
}
I tried specifying Cascade.None()
in the mapping, but I'm getting the same result. 我尝试在映射中指定
Cascade.None()
,但我得到了相同的结果。 Basically what happens is that a null
value is attempted at being inserted in the DocumentType
, and I don't want this (I want to insert null in the parent table, but I don't want to touch the child tables at all, I don't want this to cascade). 基本上会发生的事情是尝试插入
DocumentType
中的null
值,我不想这样(我想在父表中插入null,但我根本不想触及子表,我不想这个级联)。
I've also tried: .Not.Insert()
, but that didn't work either. 我也试过:
.Not.Insert()
,但这也不起作用。
I'd appreciate it if someone could help me out on this one. 如果有人可以帮我解决这个问题,我会很感激。
I guess the property DocumentType is not really null when saving. 我想保存时属性DocumentType不是真的null。 It seems there is an instance and without Cascade.All() on the reference it can not be saved.
似乎有一个实例并且没有Cascade.All()在引用上它无法保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.