ここで問題を単純化しようとしていますが、基本的には2つのエンティティをマップしようとしていますが、列がnullになる可能性があるため、データベースセットに外部キーがありません. 親に挿入しようとすると、次のエラーが発生します。
オブジェクトが保存されていない一時インスタンスを参照しています - フラッシュする前に一時インスタンスを保存するか、プロパティのカスケード アクションを自動保存するように設定してください。
これは私がこれまでに持っているものです:
私のエンティティ
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()
、しかしそれもうまくいきませんでした。
誰かがこれについて私を助けてくれれば幸いです。