NHibernate IsInitialized抛出LazyInitializationException

时间:2017-01-12 11:06:06

标签: c# nhibernate lazyload

this page读取我试图测试我的对象的属性是否已被启用

Area areaActual = null;

        using (UnitOfWork uow = GetUnitOfWork())
        {
            //ACT
            areaActual = uow.Area.List().FirstOrDefault();
        }
        //ASSERT
         NHibernateUtil.IsInitialized(areaActual.Persone).Should().BeFalse();    

但最后一行抛出此异常:

Result Message: NHibernate.LazyInitializationException : 
Initializing[Domain.Entity.Area#73]-failed to lazily initialize a collection of role: Domain.Entity.Area.Persone, no session or session was closed

我想念的是什么?

我用这种方式解决了我的问题:

[Test]
    public void AreaRepositoryCanListEagerPersone()
    {
        //Arrange
        var area = fixture.Create<Area>();

        using (UnitOfWork uow = GetUnitOfWork())
        {
            uow._session.SaveOrUpdate(area);
            uow._session.Transaction.Commit();
            uow._session.Flush();
        }
        Area areaActual = null;

        using (UnitOfWork uow = GetUnitOfWork())
        {
            //ACT
            areaActual = uow.Area.List().FirstOrDefault();
        }
        //ASSERT
        Action test = ()=> NHibernateUtil.IsInitialized(areaActual.Persone);
        test.ShouldThrow<LazyInitializationException>();

        areaActual = null;
        using (UnitOfWork uow = GetUnitOfWork())
        {
            //ACT
            areaActual = uow.Area.List(x => x.Persone).FirstOrDefault();
        }
        //ASSERT
        NHibernateUtil.IsInitialized(areaActual.Persone).Should().BeTrue();

    }

但IsInitialized方法仍然存在错误

文档似乎也是假的

0 个答案:

没有答案