简体   繁体   中英

SQLite as in-memory database for SQL Server

My setup is similar to this for testing dapper calls for SQL Server using in-memory SQLite ( http://mikhail.io/2016/02/unit-testing-dapper-repositories/ ) using this lib: https://github.com/ServiceStack/ServiceStack.OrmLite

I'm using dapper with ad hoc SQL for my DAL and wanted to test data access layer without dependency on SQL Server. I used SQLite in-memory database. Problem is SQL syntax are different between SQL Server and SQLite.

For example I have a query that returns paged results using offset and fetch next, but SQLite only supports limit and offset.

What if any suggestions you have for me to do my in memory unit test? I didn't go the EF route with mocked db context as dapper is more performant and didn't want to use stored procedures as I wanted to test my SQL as well. I'm not looking to mock my database calls.

Ormlite's Typed API is RDBMS agnostic so as long as you stick to OrmLite's Typed API you will be easily able to alternate between different databases by just changing the connection string and dialect provider, eg:

//SQL Server
var dbFactory = new OrmLiteConnectionFactory(connectionString,  
    SqlServerDialect.Provider);

//InMemory Sqlite DB
var dbFactory = new OrmLiteConnectionFactory(":memory:", 
    SqliteDialect.Provider); 

Then you can use either database to create, persist and query POCO's, eg:

using (var db = dbFactory.Open())
{
    db.DropAndCreateTable<Poco>();
    db.Insert(new Poco { Name = name });
    var results = db.Select<Poco>(x => x.Name == name);
    results.PrintDump();
}

But if use the Custom SQL API's to execute MSSQL-specific SQL you wont be able to execute that against SQLite. You can make use of the mockable support in OrmLite , but I'd personally recommend sticking to OrmLite's RDBMS agnostic typed API's instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM