NHibernate / FluentNHibernate和SQLite在内存中进行映射测试

时间:2012-03-14 16:47:22

标签: .net sqlite nhibernate fluent-nhibernate

我第一次尝试使用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

可能是什么?

2 个答案:

答案 0 :(得分: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内存中配置在会话之间不是持久的,因此您需要为每个测试执行模式导出(可能有一个配置选项来覆盖它,不确定)。

答案 1 :(得分:1)

我设法让它发挥作用。我意识到我需要在调用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