2011-12-23 55 views
1

我有點迷失在這裏。內存中的SQLite和NHibernate

我搜索了一些信息,顯然有幾個SQLite GUI工具來創建SQLite數據庫文件。同時我也注意到NHibernate的自帶SQLite的

return Fluently.Configure().Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyEntityMap>()).Database(SQLiteConfiguration.Standard.InMemory().ShowSql()).BuildSessionFactory(); 

的內存中的配置有了這樣的設置沒有選擇使用DB文件。那麼,在我使用NHibernate執行所有CRUD操作之前,如何創建所有表結構並將記錄插入到內存數據庫中?

由於

編輯1 - 包括映射類和會話類 我的實體基類

Public MustInherit Class SingleKeyEntity(Of TId) 
    Public Overridable Property Id() As TId 
     Get 
      Return m_Id 
     End Get 
     Set(value As TId) 
      m_Id = value 
     End Set 
    End Property 
End Class 

我的實體類

Public Class Subscription 
    Inherits SingleKeyEntity(Of System.Nullable(Of Integer)) 

    Private m_Subscriber As String 
    Public Overridable Property Subscriber() As String 
     Get 
      Return m_Subscriber 
     End Get 
     Set(value As String) 
      m_Subscriber = value 
     End Set 
    End Property 

    Private m_Format As String 
    Public Overridable Property Format() As String 
     Get 
      Return m_Format 
     End Get 
     Set(value As String) 
      m_Format = value 
     End Set 
    End Property 

末級

我的映射類

Public Class SubscriptionMap 
    Inherits ClassMap(Of Subscription) 
    Public Sub New() 
     Table("SUBSCRIPTIONS") 

     Id(Function(x) x.Id, "ID").GeneratedBy.Identity() 
     Map(Function(x) x.Subscriber, "SUBSCRIBER").[Not].Nullable() 
     Map(Function(x) x.Format, "DISTRIBUTION_FORMAT").[Not].Nullable() 
     ' more properties omitted 
    End Sub 
End Class 

我的會話配置

Return Fluently.Configure() _ 
     .Mappings(Function(m) m.FluentMappings.AddFromAssemblyOf(Of SubscriptionMap)()) _ 
     .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()) _ 
     .ExposeConfiguration(Sub(x As NHibernate.Cfg.Configuration) 
           Dim export As SchemaExport = New SchemaExport(x) 
           'export.Execute(False, True, False) 
           export.Create(False, True) 
          End Sub) _ 
     .BuildSessionFactory() 

回答

3

如果您使用的是InMemoryDB,則不會創建phsyical文件,並且在處理SessionFactory時,InMemoryDB將會丟失。

這使得InMemoryDB非常適合單元測試,因爲它無需安裝/拆卸所有的時間。

我希望你的目的,如果進行測試,而不是一個真正的應用程序:)

創建所有的表,那麼你就需要將配置導出像這樣:

return Fluently.Configure() 
       .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyEntityMap>()) 
       .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()).BuildSessionFactory() 
       .ExposeConfiguration(x => 
       { 
        new SchemaExport(x).Execute(false, true); 
       }); 
+0

因此,如果我需要填充一些數據到內存中,我將不得不使用NH來插入,對吧? – hardywang 2011-12-23 14:31:44

+0

@hardywang - 是的,這是正確的。但上面的內容至少會根據你的映射爲你創建所有的表格。 – Phill 2011-12-23 14:40:17

1

我猜你真的想要一個內存數據庫專門爲測試pourpose。如果是這樣,你可以在你的測試初始化​​有NHibernate的創建模式爲您提供:

SchemaExport se = new SchemaExport(cfg); 
      se.Create(true, true); 

那麼你就可以開始添加與實體播放。

+1

我想無論是'SchemaExport工具。 Execute'或'SchemaExport.Create',但似乎並沒有在我的內存表中創建模式。因爲我嘗試讀取或插入表格,並得到「SQLite錯誤沒有這樣的表:我的表名」錯誤。我只是覺得有一個簡單的方法可以檢查內存表狀態嗎? – hardywang 2011-12-23 16:38:30

+0

它應該工作。你是如何創建映射的?你如何創建cfg?請將其添加到您的問題。 – 2011-12-23 16:41:54

+0

請看我上面的問題,我包括實體類,流利的nhibernate映射類和配置 – hardywang 2011-12-23 16:54:03