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

标签 .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

可能是什么?

最佳答案

尝试在创建 session 后调用SchemaExport.Execute,以使其创建表。以下是我的 C# 单元测试代码的摘录:

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

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

关于.net - NHibernate/FluentNHibernate和SQLite在内存中进行映射测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9706288/

相关文章:

c# - 从 MMF 访问 SQLite 数据库

nhibernate - NHibernate 中如何处理并发?

c# - Lambda 方法来填充 ToDictionary() 方法中的值字段?

php - 在 MicroApache 中运行 SQLite 3

SQLite 表约束 - 在多列上唯一

asp.net-mvc - 已经使用 nhibernate.current_session 的键配置了 session 工厂

nhibernate - 为什么我在构建 NHibernate session 工厂时会得到 "read-only cache configured for mutable"?

c# - C# 命令行中的自定义主入口点参数

.net - 负载平衡 WCF 并共享远程 MSMQ 以实现高吞吐量

C# 可空相等操作,为什么 null <= null 解析为 false?