繁体   English   中英

非空外键的流畅 nhibernate 映射(HasMany)

[英]fluent nhibernate mapping with not null foreign key (HasMany)

我在 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; }
  • NHibernate 中的反向属性

    HasMany(map => map.Children).Table("ChilrenTable")
       .KeyColumn("ParentId") // this is not nullable.
       .Inverse()  // <--- This is the key here
       .Cascade
       .AllDeleteOrphan();
    

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM