【问题标题】:In-Memory SQLite and NHibernate内存中的 SQLite 和 NHibernate
【发布时间】:2011-12-23 14:08:45
【问题描述】:

我有点迷路了。

我搜索了一些信息,似乎有几个 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——包括映射类和会话类 我的实体基类

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

【问题讨论】:

    标签: nhibernate sqlite in-memory-database


    【解决方案1】:

    如果您使用的是 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);
                   });
    

    【讨论】:

    • 所以如果我需要将一些数据填充到 In Memory Db 中,我将不得不使用 NH 来插入,对吧?
    • @hardywang - 是的,这是正确的。但以上内容至少会根据您的映射为您创建所有表。
    【解决方案2】:

    我猜你真的想要一个专门用于测试目的的内存数据库。如果是这样,您可以在测试初始化​​ NHibernate 中为您创建架构:

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

    然后你就可以开始添加和玩实体了。

    【讨论】:

    • 我尝试了SchemaExport.ExecuteSchemaExport.Create,但它似乎没有在我的内存表中创建架构。因为我尝试读取或插入表并得到“SQLite 错误没有这样的表:我的表名”错误。我只是觉得有一个简单的方法可以检查内存表的状态吗?
    • 它应该可以工作。您是如何创建映射的?你如何创建cfg?请将其添加到您的问题中。
    • 请看我上面的问题,我包括了实体类,流畅的nhibernate映射类和配置
    • 对于 InMemory 数据库,您必须在与要执行的查询相同的会话中导出架构。看stackoverflow.com/questions/189280/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多