2012-03-14 63 views
1

我試圖使用SQLite和NHibernate的第一次測試我的映射,但是我得到這個錯誤:NHibernate的/ FluentNHibernate和SQLite在內存映射測試

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, "[email protected]") 
     .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 

可能是什麼?

回答

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 
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的,所以你需要執行的模式出口爲每個測試(有可能是一個配置選項來覆蓋這一點,不知道)。

+0

不要在最後一個參數 – 2012-03-14 17:16:20

+0

使用接受NULL後建立我的模式'Console.Out' – 2012-03-14 18:25:16

+0

不起作用:「FluentNHibernate.Cfg.FluentConfigurationException:一個在創建SessionFactory時使用了無效或不完整的配置。請參閱PotentialReasons集合和InnerException以獲取更多詳細信息。「 – 2012-03-15 12:42:27