nhibernateには次のマッピングがあります。電話をかけるSession.Merge(myparent)
と、挿入でエラーが発生し、外部キー( )列NULL
に挿入できないことを示します。ParentItemId
親キーが挿入時に挿入されるようにマッピングを調整するにはどうすればよいですか。外部キーをnull許容にすると、このマッピングは機能しますが、2つの別個のステートメントがデータベースに発行されます。
この関係は、子クラスの親への参照がない1対多です。
HasMany(map => map.Children).Table("ChilrenTable")
.KeyColumn("ParentItemId") // this is not nullable.
.Cascade
.AllDeleteOrphan();
更新例
// entity here is the parent instance, which contains the list
// of children.
using (var tx = Session.BeginTransaction())
{
entity = Session.Merge(entity); // this line causes the problem.
Session.SaveOrUpdate(entity);
Session.Flush();
tx.Commit();
return entity;
}