1

我有点迷失在这里。

我搜索了一些信息,似乎有几个 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()
4

2 回答 2

3

如果您使用的是 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);
               });
于 2011-12-23T14:20:22.923 回答
1

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

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

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

于 2011-12-23T14:20:13.633 回答