将 TransactionScope IsolationLevel 更改为 Inmemory DB 中的快照

Changing the TransactionScope IsolationLevel to Snapshot in Inmemory DB

我正在使用内存数据库(使用 ServiceStack.OrmLite.Sqlite.Windows)在基于 servicestack 的 Web API 中进行单元测试。我尝试测试的方法如下。

public object Get(object request)
{
    using (var db = HostContext.Resolve<IDbConnectionFactory>().OpenDbConnection("ConnectionString"))
    {                           
        using (var dbtran = db.OpenTransaction(IsolationLevel.Snapshot))
        {
            // reading operation from DB
            return response;
        }
    }
}

当我尝试使用 InmemoryDB 连接测试此方法时,由于 IsolationLevel 而出现以下异常。

An exception of type 'System.ArgumentException' occurred in System.Data.SQLite.dll but was not handled in user code

我尝试在创建 inmemoryDB 时将隔离级别设置为快照,如下所示,

var isolationlevel = IsolationLevel.Snapshot;
db.OpenTransaction().IsolationLevel.Add(isolationlevel);

即使在执行此操作之后,事务级别仍显示为可序列化并获得相同的异常。

是否有任何其他方法可以将事务隔离级别设置为内存数据库中的快照?

Sqlite 不支持创建 IsolationLevel.Snapshot 事务,但 SQLite's Isolation and Concurrency docs indicates SQLite exhibits "snapshot isolation" when Write Ahead Logging (WAL) 模式由 运行 "PRAGMA journal_mode=WAL" 启用,您可以在 OrmLite 中设置:

db.ExecuteSql("PRAGMA journal_mode=WAL");