· 1 min read

Using In Memory Database Context for an Integration Test

I had a requirement to access the data of an in memory database context for an integration test.

So for example some seeded data created in the CustomWebApplicationFactory needed to be accessed as part of an integration test.

Here’s how it can be done within an integration test:

using Microsoft.Extensions.DependencyInjection;

...

var scopeFactory = _factory.Server.Host.Services.GetService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
  // Obtain in memory db context
  var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
}
Back to Blog

Related Posts

View All Posts »
Paging In .Net Core with C# and Linq

Paging In .Net Core with C# and Linq

Almost every medium to large site requires some sort of paging through lists of information. The advantage of paging is that you only need to bring back a limited result set.

How to Connect to a DigitalOcean Managed Database When Your IP Address Changes

How to Connect to a DigitalOcean Managed Database When Your IP Address Changes

If you're working with a DigitalOcean managed database from your local machine, you've probably run into this frustrating scenario: everything works perfectly one day, then suddenly your database connection times out. The culprit? Your IP address changed, and it's no longer in the database's trusted sources list.