我们正在构建一个翻译应用程序,它从一个数据库中读取数据并将其翻译成完全不同的格式。我们将使用 NHibernate 从尚不可用的源数据库中读取。我们想通过模拟一个源对象来测试翻译核心逻辑,将它传递给我们的翻译核心,并验证输出。每次我们尝试将项目添加到一对多集合(声明为 PersistentGenericBag)时,我们都会得到 NHibernate.LazyInitializationException: Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session is closed。有没有人解决这个问题?
private PersistentGenericBag<Child> _Children = null;
[NHibernate.Mapping.Attributes.Bag(1, Name = "Children", Table = "Children", Lazy = CollectionLazy.False, Cascade = "all")]
[NHibernate.Mapping.Attributes.Key(2)]
[NHibernate.Mapping.Attributes.Column(3, Name = "ParentId")]
[NHibernate.Mapping.Attributes.OneToMany(4, ClassType = typeof(Child))]
public virtual PersistentGenericBag<Child> Children
{
get
{
if (_Children == null)
{
_Children = new PersistentGenericBag<Child>();
}
return _Children;
}
set { _Children = value; }
}
[TestMethod]
public void Xyz()
{
Parent parent = new Parent ();
parent.Children.Add(new Child()); //exception thrown here
Assert.AreEqual(parent.Children.Count, 1);
}