我有以下代码:
var rootFolder = new KnowledgeFolder { Archived = true, Name = path.First()};
this._folderRepository.Save(rootFolder); // this goes to the db
然后:(不相关的代码不在这里)
var child = new KnowledgeFolder { Name = entry, Archived = true };
rootFolder.Children.Add(child);
//////////
this._folderRepository.FlushSession();
rootFolder已正确保存到数据库,但我稍后添加到“儿童”的其他项目将无法保存。为什么?我知道如果以前从数据库下载了根文件夹,这将有效。为什么NHibernate不够智能来检查对象图?
//修改
使用流畅的api完成映射:
mapping.HasManyBidirectional<KnowledgeFolder, KnowledgeFolder>(g => g.Children, p => p.Parent);
答案 0 :(得分:1)
你没有在子项上设置父项,所以nhibernate不知道关系的两个方面。
var child = new KnowledgeFolder { Name = entry, Archived = true };
rootFolder.Children.Add(child);
child.Parent = rootFolder;