nhibernate - 内存中的 SQLite 和 NHibernate

标签 nhibernate sqlite in-memory-database

我有点迷失在这里。

我搜索了一些信息,似乎有几个 SQLite GUI 工具可以创建 SQLite DB 文件。同时我还注意到 NHibernate 带有 SQLite 的内存配置

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

有了这样的设置,就没有使用 DB 文件的选项了。那么,在使用 NHibernate 执行所有 CRUD 操作之前,如何先创建所有表结构并将记录插入内存数据库?

谢谢

编辑 1——包括映射类和 session 类
我的实体基类
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

结束类

我的 map 课
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

还有我的 session 配置
    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()

最佳答案

如果您使用的是 InMemoryDB,则不会创建任何物理文件,并且当 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);
               });

关于nhibernate - 内存中的 SQLite 和 NHibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8616993/

相关文章:

python - lru_cache 转储到文件中并再次加载回内存中

php - Redis CLI 未通过 Laravel 显示最近存储的 key

java - 将现有的 SQLite 数据库加载到内存

c# - 修改nHibernate在nServiceBus中保存Saga数据的方式

Nhibernate 查询结束

.net - 需要良好的Nhibernate Criteria教程

java - 如何在 Android 模拟器上访问现有的 sqlite 数据库?

nhibernate - 持久性规范和逆

iphone - HTML5 Webapp 作为 iPhone 和 Android 上的常规图标或应用程序。

Android sqlite 和多个查询