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

时间:2016-06-17 10:02:07

标签: c# sqlite ormlite-servicestack in-memory-database

我正在使用inMemory数据库(使用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);

即使在执行此操作后,事务级别也显示为Serializable,并获得相同的异常。

有没有其他方法可以在inmemoryDB中将事务隔离级别设置为快照?

1 个答案:

答案 0 :(得分:2)

Sqlite不支持创建IsolationLevel.Snapshot事务,但SQLite's Isolation and Concurrency docs表示当运行“PRAGMA journal_mode = WAL”启用Write Ahead Logging(WAL)模式时,SQLite会展示“快照隔离”您可以在OrmLite中设置:

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