Tutorials ASP.NET Core Complete Tutorial (ShopNest)

Testing EF Core — In-Memory vs SQLite

Learn Testing EF Core — In-Memory vs SQLite in our free ASP.NET Core Complete Tutorial (ShopNest) series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
Testing EF Core — In-Memory vs SQLite — ShopNest
Article 56 of 75 · Module 7: Testing · ShopNest Repository Layer Testing
Target keyword: testing ef core · Read time: ~30 min · .NET: 8 / 9 · Project: ShopNest Repository Layer Testing

Introduction

ShopNest repository tests need a database — EF InMemory is fast but lies about SQL behavior; SQLite in-memory runs real SQL and catches more bugs.

After this article you will

  • Compare EF InMemory vs SQLite test providers
  • Seed test data and test complex LINQ queries
  • Choose fresh DbContext per test vs shared
  • Test repository and specification implementations
  • Reset databases with Respawn

Prerequisites

Concept deep-dive

ProviderProsCons
EF InMemoryFastest, no SQLNo FK constraints, different semantics
SQLite memoryReal SQL, relationalNot 100% SQL Server identical
SQL Server LocalDBClosest to prodSlower, CI setup harder
private ShopNestDbContext CreateContext()
{
    var options = new DbContextOptionsBuilder<ShopNestDbContext>()
        .UseSqlite("DataSource=:memory:")
        .Options;
    var ctx = new ShopNestDbContext(options);
    ctx.Database.OpenConnection();
    ctx.Database.EnsureCreated();
    SeedTestProducts(ctx);
    return ctx;
}

[Fact]
public async Task GetByRegion_ReturnsFilteredCustomers()
{
    using var ctx = CreateContext();
    var repo = new CustomerRepository(ctx);
    var spec = new CustomersByRegionSpec("South");
    var result = await repo.ListAsync(spec);
    Assert.All(result, c => Assert.Equal("South", c.Region));
}

Hands-on — ShopNest Repository Layer Testing

  1. Repository tests with SQLite :memory: per test.
  2. Test Include/specification SQL-heavy queries.
  3. Document InMemory failures (Ignore FK) you hit once.
  4. Respawn reset for integration test SQL Server.

Common errors & best practices

  • InMemory for testing transactions — not supported reliably.
  • Shared context mutated across parallel tests — disable parallel or isolate.
  • Assuming SQLite = SQL Server — test critical queries on real SQL in CI.

Interview questions

Q: Why not InMemory?
A: Ignores constraints and provider-specific SQL — SQLite closer to real.

Q: EnsureCreated vs Migrate?
A: EnsureCreated for tests; Migrate if testing migration scripts.

Summary

  • Prefer SQLite in-memory over EF InMemory for repos
  • Seed minimal data per test for clarity
  • Test specifications against real LINQ-to-SQL
  • Respawn cleans SQL Server between integration tests

Previous: Integration Testing
Next: Performance and Load Testing

FAQ

Test migrations?

Use Migrate() against LocalDB or Testcontainers SQL.

Parallel repo tests?

Unique database name per test collection.

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ASP.NET Core Complete Tutorial (ShopNest)
Course syllabus
Module 1: Foundations
Module 2: Entity Framework Core
Module 3: Dependency Injection & Middleware
Module 4: Authentication & Security
Module 5: Web API
Module 6: Advanced Architecture
Module 7: Testing
Module 8: Deployment & DevOps
Module 9: Real-World Projects
Module 10: Advanced Topics
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details