1

私はここでちょっと迷っています。

調べてみると、SQLite DBファイルを作成するためのSQLite GUIツールがいくつかあるようです。一方、NHibernate には SQLite のメモリ内構成が付属していることにも気付きました

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

このような設定では、DB ファイルを使用するオプションはありません。では、NHibernate を使用してすべての CRUD 操作を実行する前に、まずすべてのテーブル構造を作成し、インメモリ データベースにレコードを挿入するにはどうすればよいでしょうか?

ありがとう

編集 1 -- マッピング クラスとセッション クラスを含める My エンティティ ベース クラス

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 に答える