あるデータベースからデータを読み取り、それをまったく異なる形式に変換する翻訳アプリケーションを構築しています。NHibernate を使用して、まだ利用できないソース データベースから読み取ります。ソース オブジェクトをダミーで作成し、それを翻訳コアに渡し、出力を検証することで、翻訳コア ロジックをテストしたいと考えています。1 対多のコレクション (PersistentGenericBag として宣言されている) に項目を追加しようとするたびに、NHibernate.LazyInitializationException: Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was 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);
}