“Session is Closed!” - NHibernate

六眼飞鱼酱① 提交于 2019-12-05 07:56:50

I haven't tested your code, but from reading, this line:

using (ISession session = SessionFactory.Instance.GetCurrentSession())

is dumping your session after the block exits, and then the session is disposed/invalid on the next time through.

Here's the model we're using in our applications:

ISession session = null;

try
{
    // Creates a new session, or reconnects a disconnected session
    session = AcquireCurrentSession();

    // Database operations go here
}
catch
{
    session.Close();
    throw;
}
finally
{
    session.Disconnect();
}

I got a similar error. Turns out I was "new"ing up my repository instead of having my IOC container resolve it.

Ashutosh Mishra

Use of the following statement disposes off or closes the session after every query:

using (ISession session = SessionFactory.Instance.GetCurrentSession())

Instead use it without the "using" word as:

ISession session = SessionFactory.Instance.GetCurrentSession()

This worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!