NHibernate 当前 session 上下文问题

标签 nhibernate fluent-nhibernate nhibernate-3

我最近从直接使用 ISession 转向了包装的 ISession,即工作单元类型模式。

我曾经使用 SQL Lite(内存中)对此进行测试。我有一个简单的帮助器类,它配置我的 SessionFactory,创建一个 ISession,然后使用 SchemaExport 构建架构,然后返回我的 ISession 并且架构一直存在,直到我关闭 session 。我已经稍微改变了这一点,以便我现在配置一个 SessionFactory,创建一个 ISession,构建模式,并将工厂传递给我的 NHibernateUnitOfWork 并将其返回给我的测试。

var databaseConfiguration =
                SQLiteConfiguration.Standard.InMemory()
                .Raw("connection.release_mode", "on_close")
                .Raw("hibernate.generate_statistics", "true");

            var config = Fluently.Configure().Database(databaseConfiguration).Mappings(
                m =>
                {
                    foreach (var assembly in assemblies)
                    {
                        m.FluentMappings.AddFromAssembly(assembly);
                        m.HbmMappings.AddFromAssembly(assembly);
                    }
                });

            Configuration localConfig = null;
            config.ExposeConfiguration(x =>
                {
                    x.SetProperty("current_session_context_class", "thread_static"); // typeof(UnitTestCurrentSessionContext).FullName);
                    localConfig = x;
                });

            var factory = config.BuildSessionFactory();
            ISession session = null;

            if (openSessionFunction != null)
            {
                session = openSessionFunction(factory);
            }

            new SchemaExport(localConfig).Execute(false, true, false, session.Connection, null);

            UnitTestCurrentSessionContext.SetSession(session);

            var unitOfWork = new NHibernateUnitOfWork(factory, new NHibernateUTCDateTimeInterceptor());
            return unitOfWork;

在内部,NHibernateUnitOfWork 需要获取用于创建模式的 ISession,否则内存数据库实际上不会有模式,因此这是它调用以获取 ISession 的方法。
private ISession GetCurrentOrNewSession()
        {
            if (this.currentSession != null)
            {
                return this.currentSession;
            }

            lock (this)
            {
                if (this.currentSession == null)
                {
                    // get an existing session if there is one, otherwise create one
                    ISession session;
                    try
                    {
                        session = this.sessionFactory.GetCurrentSession();
                    }
                    catch (Exception ex)
                    {
                        Debug.Write(ex.Message);
                        session = this.sessionFactory.OpenSession(this.dateTimeInterceptor);
                    }

                    this.currentSession = session;
                    this.currentSession.FlushMode = FlushMode.Never;
                }
            }

问题是this.sessionFactory.GetCurrentSession总是抛出异常说 ICurrentSessionContext未注册。

我尝试了多种不同的方法来设置属性和不同的值(如您在上面看到的,“thread_static”和我自己的 ICurrentSessionContext ),但似乎都不起作用。

任何人都有任何建议

最佳答案

尝试这个:

try
{
    if (NHibernate.Context.CurrentSessionContext.HasBind(sessionFactory))
    {
        session = sessionFactory.GetCurrentSession();
    }
    else
    {
        session = sessionFactory.OpenSession(this.dateTimeInterceptor);
        NHibernate.Context.CurrentSessionContext.Bind(session);
    }
}
catch (Exception ex)
{
    Debug.Write(ex.Message);
    throw;
}

关于NHibernate 当前 session 上下文问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5914405/

相关文章:

c# - NHIbernate <any> 映射问题

sql-server-2008 - Fluent NHibernate - 总是删除表

asp.net 自定义角色提供程序 nhibernate 错误

c# - 帮助使用 FluentNHibernate 创建 ColumnName 约定

c# - nHibernate session 和多线程

c# - 流畅的nhibernate级联删除到已连接子类的集合

c# - Fluent Nhibernate - 通过分组连接查询

NHibernate RowCountInt64 使用转换后的查询返回错误的计数

wcf - NHibernate + WCF + Windows 服务和 WcfOperationSessionContext 类

linq - NHib 3 配置和映射返回空结果?