Search code examples
sqliteservicestackormlite-servicestack

ServiceStack OrmLite using Sqlite64 as memory database results in missing auth tables


I am trying to use Sqlite as a memory database with ServiceStack ORMlite in my unit tests.

If I run my tests with SQLite saving to a file, ie. using the connectionstring

"Data Source=|DataDirectory|unittest.db;Version=3;"

, it works fine and auth tables are generated fine by ServiceStacks

userRepository.CreateMissingTables(); 

However, when I try using SQLite as a memory database by using this connectionstring

":memory:"

I get an exception when saying

SQLite error
no such table: UserAuth

the first time I try to fetch a user by doing this

userRepository.GetUserAuthByUserName(...)

This is after I have called userRepository.CreateMissingTables() and it works fine if I switch to using SQLite with a file database. Does anybody know what the problem could be? (I had to down grade to version 3.9.0 of ORMLite because of bad references to version 1.0.65.0 of ORM lite in Ormlite 3.9.4)


Solution

  • ServiceStack v5

    Recent versions of ServiceStack automatically disables AutoDisposeConnection for SQLite :memory: connections, so you can configure your OrmLiteConnectionFactory normally, e.g:

    var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
    
    var sqliteRepo = new OrmLiteAuthRepository(dbFactory);
    sqliteRepo.CreateMissingTables();
    

    ServiceStack v3

    You lose your database whenever you close the DB connection of an Sqlite in memory database.

    So when you configure your DB Factory you need to tell it to never dispose of the connection, which you can do with the autoDisposeConnection constructor param, e.g:

    var dbFactory = new OrmLiteConnectionFactory(":memory:", 
        autoDisposeConnection:false, 
        dialectProvider:SqliteDialect.Provider);
    
    var sqliteRepo = new OrmLiteAuthRepository(dbFactory);
    sqliteRepo.CreateMissingTables();