3

我在 nhibernate 中有以下映射。当我打电话时,Session.Merge(myparent)我在插入时收到一个错误,表明NULL无法插入外键 ( ParentItemId) 列。

如何调整映射以便在插入时插入父键。如果我使外键可以为空,则此映射有效,但会向数据库发出两个单独的语句。

这种关系是一对多的,没有对子类的父级的引用。

  • 子类没有父属性。
  • 孩子依赖父母。

    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 回答