Problem using SQLite :memory: with NHibernate

前端 未结 9 1086
春和景丽
春和景丽 2020-12-02 08:28

I use NHibernate for my dataacess, and for awhile not I\'ve been using SQLite for local integration tests. I\'ve been using a file, but I thought I would out the :memory: o

相关标签:
9条回答
  • 2020-12-02 09:07

    Just a wild guess, but is the sql output by NHibernate using a command unsupported by sqlite?

    Also, What happens if you use a file instead of memory? (System.IO.Path.GetTempFileName() would work i think...)

    0 讨论(0)
  • 2020-12-02 09:09

    We are using SQLite in memory for all our database tests. We are using a single ADO connection for the tests that is reused for all NH sessions opened by the same test.

    1. Before every test: create connection
    2. Create schema on this connection
    3. Run test. The same connection is used for all sessions
    4. After test: close connection

    This allows also running tests with several sessions included. The SessionFactory is also created once for all tests, because the reading of the mapping files takes quite some time.


    Edit

    Use of the Shared Cache

    Since System.Data.Sqlite 1.0.82 (or Sqlite 3.7.13), there is a Shared Cache, which allows several connections to share the same data, also for In-Memory databases. This allows creation of the in-memory-database in one connection, and use it in another. (I didn't try it yet, but in theory, this should work):

    • Change the connection string to file::memory:?cache=shared
    • Open a connection and create the schema
    • Keep this connection open until the end of the test
    • Let NH create other connections (normal behavior) during the test.
    0 讨论(0)
  • 2020-12-02 09:09

    I got the same error, when I forgot to import the SQLite Nuget package.

    0 讨论(0)
提交回复
热议问题