简体   繁体   English

SQLite作为SQL Server的内存数据库

[英]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 我的设置类似于使用内存SQLite( http://mikhail.io/2016/02/unit-testing-dapper-repositories/ )使用此lib来测试SQL Server的dapper调用: 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. 我正在为我的DAL使用dapper和ad hoc SQL,并希望测试数据访问层而不依赖于SQL Server。 I used SQLite in-memory database. 我使用了SQLite内存数据库。 Problem is SQL syntax are different between SQL Server and SQLite. 问题是SQL语法和SQLite之间的SQL语法不同。

For example I have a query that returns paged results using offset and fetch next, but SQLite only supports limit and offset. 例如,我有一个查询,使用offset和fetch next返回分页结果,但SQLite仅支持limit和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. 我没有使用模拟db上下文的EF路由,因为dapper性能更高,并且不想使用存储过程,因为我也想测试我的SQL。 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: Ormlite的Typed API与RDBMS无关,因此只要您坚持使用OrmLite的Typed API,您就可以通过更改连接字符串和方言提供程序轻松地在不同数据库之间切换,例如:

//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: 然后,您可以使用任一数据库来创建,保留和查询POCO,例如:

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. 但是如果使用Custom SQL API来执行特定于MSSQL的SQL ,则无法对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. 您可以使用OrmLite中的可模拟支持 ,但我个人建议坚持使用OrmLite的RDBMS不可知类型的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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