我在 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;
}