繁体   English   中英

没有外键的流畅的NHibernate关系

[英]Fluent NHibernate Relation Without Foreign Key

我正在尝试这样简化我的问题,但基本上我正在尝试映射2个实体但是我没有数据库集中的外键,因为列可能为null。 当我尝试在父项上插入时,我收到以下错误:

object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例,或者将属性的级联操作设置为使其自动保存的内容。

这是我到目前为止:

我的实体

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() ,但我得到了相同的结果。 基本上会发生的事情是尝试插入DocumentType中的null值,我不想这样(我想在父表中插入null,但我根本不想触及子表,我不想这个级联)。

我也试过: .Not.Insert() ,但这也不起作用。

如果有人可以帮我解决这个问题,我会很感激。

我想保存时属性DocumentType不是真的null。 似乎有一个实例并且没有Cascade.All()在引用上它无法保存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM