【发布时间】:2012-09-05 07:08:43
【问题描述】:
我在 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;
}
【问题讨论】:
标签: c# nhibernate fluent-nhibernate