Nhibernate不将parentid插入子级 [英] Nhibernate not inserting parentid into child
问题描述
我手头有一个奇怪的问题.首先看看我的表架构.
I have a weird problem at hand. First have a look at my table schema.
A(ID) B(ID,AID) C(ID,AID) D(ID,CID)
A(ID) B(ID,AID) C(ID,AID) D(ID,CID)
地图文件如下:
MAP A
{
HasMany(x => x.B).Cascade.AllDeleteOrphan().Inverse().KeyColumn("AID");
HasMany(x => x.C).Cascade.AllDeleteOrphan().Inverse().KeyColumn("AID");
}
MAP B
{
References(x => x.A).Column("AID");
}
MAP C
{
References(x => x.A).Column("AID");
HasMany(x => x.D).Cascade.AllDeleteOrphan().Inverse().KeyColumn("BID");
}
MAP D
{
References(x => x.C).Column("CID");
}
在A上执行SaveORUpdate/Merge时,它不会将AID插入到B和C中.但是,它确实将CID插入到D中.任何建议.
While doing SaveORUpdate/Merge on A it doesn't insert AID into B and C. But it does insert CID into D. Any suggestions.
推荐答案
如果发生这种情况,您肯定会错过分配关系的双方.如果可以的话:
If this happens, you for sure missed to assign both sides of relation. If this would be in place:
var parent = ...;
var child = ...;
parent.Children.Add(child);
child.Parent = parent;
一切正常.因为这里最令人怀疑的是您的代码是这样的:
All will work. Because the most supsected here is that your code is like:
var parent = ...;
var child = ...;
parent.Children.Add(child);
// child.Parent = parent; // this is missing
,并且不会插入子代.为什么?
and that won't insert children. Why?
因为我们使用了 .Inverse()
映射.这是一个非常强大但又脆弱的设置.它允许NHibernate进行一些重要的优化,但这需要-必须在子级中设置PARENT .
查看这篇不错的文章
Inverse ="true"示例和说明 由mykong
Inverse = "true" example and explanation by mykong
这篇关于Nhibernate不将parentid插入子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!