0

クラスとそれらの間の1対多の関係が必要です。

EstimateVersion.cs

private ISet<Template> _templates;
public virtual ISet<Template> Templates
{
    get { return _templates ?? (_templates = new HashedSet<Template>()); }
    set { _templates = value; }
}

Template.cs

public virtual EstimateVersion EstimateVersion { get; set; }

以下は、マッピング ファイルで定義されたそれらの間の関係を示しています。

EstimateVersion.hbm.xml

<set name="Templates" table="EST_TTemplate" cascade="all-delete-orphan" schema="{TRAN_USER}" inverse="true">
  <key column="EstimateVersionId" />
  <one-to-many class="Template" />
</set>

Template.hbm.xml

<many-to-one name="EstimateVersion" class="EstimateVersion" column="EstimateVersionId" />

を作成する私のコードではEstimateVersion、これがオブジェクト間の関係を「オブジェクトに知らせる」方法です。

var version = new EstimateVersion();
//Code that inserts values into the object's properties
Repository.Save(version);
var template = new Template();
//Code that inserts values into the object's properties
Repository.Save(template);
template.EstimateVersion = version;

見積もりバージョンを挿入するクエリは正常に実行されますが、テンプレートレコードを挿入するときに、EstimateVersionId に null を挿入しようとし、null 非許容であるためエラーをスローします。(null可能であれば、最初にnullとして挿入してから、正しい値で更新すると思います)。

どうすればこれを修正できますか?

4

1 に答える 1

2

Secret Squirrel が言ったように、行は逆にする必要があります。最初に Template オブジェクトに EstimateVersion を設定すると、更新によって外部キー リンクが保存され、null 値の挿入はまったく試みられなくなります。

したがって、コード サンプルは次のようになります。

var version = new EstimateVersion();
//Code that inserts values into the object's properties
Repository.Save(version);
var template = new Template();
//Code that inserts values into the object's properties
template.EstimateVersion = version;
Repository.Save(template);
于 2013-08-09T13:12:15.173 に答える