【问题标题】:NHibernate/FluentNHibernate and SQLite in memory for mapping test内存中的 NHibernate/FluentNHibernate 和 SQLite 用于映射测试
【发布时间】:2012-03-14 16:47:21
【问题描述】:

我第一次尝试使用 SQLite 和 NHibernate 来测试我的映射,但我收到了这个错误:

Test method BMGChip.Tests.clsCorrespondenteMapTest.Can_correctly_map_Correspondente threw exception: 
NHibernate.Exceptions.GenericADOException: could not insert: [BMGChip.NHibernate.Entities.clsCorrespondente][SQL: INSERT INTO CPHSITE12_COR (COR_NOM, COR_EMA, COR_TEL, COR_RUA, COR_NUM, COR_COM, COR_CID, COR_EST, COR_CEP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); select last_insert_rowid()] ---> System.Data.SQLite.SQLiteException: SQLite error
no such table: CPHSITE12_COR

我正在尝试为每个测试方法创建和删除数据库。

我的 NHibernate 配置:

Public Class clsSessionFactoryBuilder
    Private Shared _sessionFactory As ISessionFactory

    Private Shared Function GetSessionFactory() As ISessionFactory
        If _sessionFactory Is Nothing Then
            _sessionFactory = Fluently.Configure() _
                .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of clsFaleConoscoMap)()) _
                .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()) _
                .ExposeConfiguration(Function(cfg) ExportarSchema(cfg)) _
                .ExposeConfiguration(Function(cfg) cfg.SetProperty("current_session_context_class", "thread_static")) _
                .BuildSessionFactory()
        End If

        Return _sessionFactory
    End Function

    Public Shared Sub OpenSession()
        Dim session As ISession = GetSessionFactory.OpenSession
        CurrentSessionContext.Bind(session)
    End Sub

    Public Shared Function GetCurrentSession() As ISession
        Return GetSessionFactory.GetCurrentSession
    End Function

    Public Shared Sub CloseSession()
        Dim session As ISession = CurrentSessionContext.Unbind(_sessionFactory)

        If session Is Nothing Then Return

        Try
            'session.Transaction.Commit()
        Catch ex As Exception
            'session.Transaction.Rollback()
        Finally
            session.Close()
            session.Dispose()
        End Try
    End Sub

    Private Shared Function ExportarSchema(ByVal configuration As Cfg.Configuration)
        Dim export As New SchemaExport(configuration)

        export.Create(False, True)
        Return Nothing
    End Function
End Class

我的测试:

<TestMethod()>
Public Sub Can_correctly_map_Correspondente()
    clsSessionFactoryBuilder.OpenSession()

    Dim session As ISession = clsSessionFactoryBuilder.GetCurrentSession()

    With New PersistenceSpecification(Of clsCorrespondente)(session)
        .CheckProperty(Function(c) c.Nome, "Fernanda Moreira")
        .CheckProperty(Function(c) c.Email, "fernanda@moreira.com.br")
        .CheckProperty(Function(c) c.Telefone, "(31) 3131-3131")
        .CheckProperty(Function(c) c.Rua, "R. Andaluzita")
        .CheckProperty(Function(c) c.Numero, "775")
        .CheckProperty(Function(c) c.Complemento, "Do lado do Pátio Savassi")
        .CheckProperty(Function(c) c.Cidade, "Belo Horizonte")
        .CheckProperty(Function(c) c.Estado, "MG")
        .CheckProperty(Function(c) c.Cep, "44444-444")
        .VerifyTheMappings()
    End With

    clsSessionFactoryBuilder.CloseSession()
End Sub

可能是什么?

【问题讨论】:

    标签: .net sqlite nhibernate fluent-nhibernate


    【解决方案1】:

    在创建会话后尝试调用SchemaExport.Execute,以便它创建表。这是我的 C# 单元测试代码的摘录:

    new SchemaExport(configuration).Execute(
                false, // Change to true to write DDL script to console
                true,
                false,
                this.Session.Connection,
                null);
    

    还请记住,SQLIte 内存中的配置在会话之间不是持久的,因此您需要为每个测试执行架构导出(可能有一个配置选项来覆盖它,不确定)。

    【讨论】:

    • 不工作:“FluentNHibernate.Cfg.FluentConfigurationException:创建 SessionFactory 时使用了无效或不完整的配置。请检查 PotentialReasons 集合,并查看 InnerException 了解更多详细信息。”
    【解决方案2】:

    我设法使它工作。我意识到我需要在调用 BuildSessionFactory() 后构建我的模式

    Public Class clsSessionFactoryBuilder
        Private Shared sessionFactory As ISessionFactory
        Private Shared configuration As Cfg.Configuration
    
        Public Shared Function GetSessionFactory() As ISessionFactory
            If sessionFactory Is Nothing Then
                sessionFactory = Fluently.Configure() _
                    .Database(SQLiteConfiguration.Standard.InMemory) _
                    .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of clsFaleConoscoMap)()) _
                    .ExposeConfiguration(Function(c) c.SetProperty("current_session_context_class", "call")) _
                    .ExposeConfiguration(Function(c) c.SetProperty("connection.release_mode", "on_close")) _
                    .ExposeConfiguration(Function(c) PersistConfig(c)) _
                    .BuildSessionFactory()
            End If
    
            Return sessionFactory
        End Function
    
        Public Shared Function OpenSession() As ISession
            Dim session As ISession = GetSessionFactory.OpenSession
            CurrentSessionContext.Bind(session)
    
            SchemaExport(configuration)
    
            Return session
        End Function
    
        Public Shared Function GetCurrentSession() As ISession
            Return GetSessionFactory.GetCurrentSession
        End Function
    
        Public Shared Sub CloseSession()
            Dim session As ISession = CurrentSessionContext.Unbind(sessionFactory)
    
            If session Is Nothing Then Return
    
            session.Close()
            session.Dispose()
        End Sub
    
        Private Shared Function SchemaExport(ByVal configuration As Cfg.Configuration)
            Dim export As New SchemaExport(configuration)
    
            export.Execute(False, True, False, sessionFactory.GetCurrentSession.Connection, Nothing)
            Return Nothing
        End Function
    
        Private Shared Function PersistConfig(ByVal c As Cfg.Configuration)
            configuration = c
        End Function
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多