hardywang
hardywang

Reputation: 5192

In-Memory SQLite and NHibernate

I am kind of lost here.

I searched some information, appearently there are several SQLite GUI tool to create SQLite DB file. Meanwhile I also noticed NHibernate comes with an in-memory configuration of SQLite

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

With such a setting there is no option to use DB file. So how can I create all table structure and insert records to in-memory database first before I use NHibernate to perform all CRUD operations?

Thanks

Edit 1 -- included mapping class and session class My entity base class

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

My entity 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

End Class

My mapping class

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

And my session configuration

    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()

Upvotes: 1

Views: 3753

Answers (2)

Phill
Phill

Reputation: 18804

If you're using an InMemoryDB, there is no phsyical file created, and when the SessionFactory is disposed of, the InMemoryDB will be lost.

This makes the InMemoryDB great for Unit Testing as it gets rid of the need to setup/teardown all the time.

I hope your purpose if for testing and not a real application :)

To create all the tables then you will need to export the config like so:

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

Upvotes: 3

Felice Pollano
Felice Pollano

Reputation: 33272

I'm guessing you really want an in memory database exclusively for test pourpose. If so you can have in your test initialization NHibernate creating the schema for you:

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

then you can start adding and playing with entities.

Upvotes: 1

Related Questions