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.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Describe a real-world scenario where Testing mattered in a ASP.NET Core Complete Tutorial (ShopNest) project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Testing i…
Junior Detailed
Explain CLR & types in the context of ASP.NET Core Complete Tutorial (ShopNest).
Short answer: The CLR loads assemblies, manages memory (GC), and JIT-compiles IL to native code. Value types live on the stack or inline in objects; reference types live on the heap with GC tracking. Real-world example (…
Mid Detailed
What are common mistakes teams make with ASP.NET Core when using ASP.NET Core Complete Tutorial (ShopNest)?
Short answer: ASP.NET Core is cross-platform, uses Kestrel, middleware pipeline, and built-in DI. Requests flow: routing → middleware → endpoints → filters → action. Real-world example (ShopNest) In a ShopNest .NET servi…
Senior Detailed
How would you debug a production issue related to EF Core in a ASP.NET Core Complete Tutorial (ShopNest) application?
Short answer: EF Core maps C# entities to tables, tracks changes, and translates LINQ to SQL. Migrations version schema; Include/ThenInclude load graphs. Real-world example (ShopNest) In a ShopNest .NET service, explain…
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