没有会话的 NHibernate

发布于 2024-07-29 06:15:27 字数 1099 浏览 1 评论 0原文

我们正在构建一个翻译应用程序,它从一个数据库读取数据并将其翻译成一种完全不同的格式。 我们将使用 NHibernate 从尚不可用的源数据库中读取数据。 我们希望通过虚拟源对象、将其传递到我们的翻译核心并验证输出来测试翻译核心逻辑。 每次我们尝试将项目添加到一对多集合(声明为 PersistentGenericBag)时,我们都会收到 NHibernate.LazyInitializationException:Initializing[Unavailable#] - 无法延迟初始化集合,没有会话或会话被关闭。 有人解决过这个问题吗?

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);
}

We are building a translation application which reads data from one database and translates it into a completely different format. We will be using NHibernate to read from the source database which is not yet available. We want to test the translation core logic by dummying up a source object, pass it through our translation core, and validating the output. Every time we attempt to add items to a one to many collection (declared as a PersistentGenericBag) we get NHibernate.LazyInitializationException: Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was closed. Has anyone tackled this?

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-08-05 06:15:27

我同意 mausch 的观点,您应该在域模型中尝试坚持使用标准 .Net 集合元素(例如 IList),而不是 NHibernate 集合。

完成此操作后,您应该能够在集合中添加和删除项目,而不会出现任何 NHibernate 问题。

I agree with mausch, you should use try to stick to standard .Net collection elements like IList in your domain model instead of the NHibernate collections.

Once you do that you should be able to add and remove items from the collection without having any issues with NHibernate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文