3

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;
    }
    
  • 4

    1 に答える 1

    3

    NHibernateの逆属性

    HasMany(map => map.Children).Table("ChilrenTable")
       .KeyColumn("ParentId") // this is not nullable.
       .Inverse()  // <--- This is the key here
       .Cascade
       .AllDeleteOrphan();
    
    于 2012-09-12T11:43:40.610 に答える