Skip to content

Commit

Permalink
Updated support for multiple dbContext in SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
Farshad DASHTI authored and Farshad DASHTI committed Oct 13, 2024
1 parent 5b6ffda commit 2d72080
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
14 changes: 10 additions & 4 deletions src/DfE.CoreLibs.Testing/Helpers/DbContextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ public static void CreateDbContext<TContext>(IServiceCollection services, Action

if (string.IsNullOrEmpty(connectionString) || connectionString.Contains("DataSource=:memory:"))
{
var connection = new SqliteConnection(connectionString ?? "DataSource=:memory:");
// Sqlite doesn't seem to allow multiple dbContexts added to the same connection
// We are creating a separate in-memory database for each dbContext
// Please feel free to update if you have a better/ more efficient solution
var connection = new SqliteConnection("DataSource=:memory:");

connection.Open();

services.AddSingleton<DbConnection>(_ => connection);
services.AddSingleton(connection);

services.AddDbContext<TContext>((sp, options) =>
{
options.UseSqlite(sp.GetRequiredService<DbConnection>());
options.UseSqlite(connection);
});
}
else
Expand All @@ -34,8 +39,9 @@ public static void CreateDbContext<TContext>(IServiceCollection services, Action
}

var serviceProvider = services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<TContext>();

using var scope = serviceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<TContext>();
dbContext.Database.EnsureCreated();

seedTestData?.Invoke(dbContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{

var dbContextDescriptors = services
.Where(d => d.ServiceType.IsGenericType && d.ServiceType.GetGenericTypeDefinition() == typeof(DbContextOptions<>))
.ToList();
foreach (var dbContextDescriptor in dbContextDescriptors)
{
services.Remove(dbContextDescriptor);
}

var dbConnectionDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbConnection));
if (dbConnectionDescriptor != null)
{
services.Remove(dbConnectionDescriptor);
}

foreach (var entry in SeedData ?? [])
{
var dbContextType = entry.Key;
var seedAction = entry.Value;

var dbContextDescriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(DbContextOptions<>).MakeGenericType(dbContextType));
if (dbContextDescriptor != null)
{
services.Remove(dbContextDescriptor);
}

var dbConnectionDescriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(DbConnection));
if (dbConnectionDescriptor != null)
{
services.Remove(dbConnectionDescriptor);
}


var createDbContextMethod = typeof(DbContextHelper).GetMethod(nameof(DbContextHelper.CreateDbContext))
?.MakeGenericMethod(dbContextType);

Expand Down

0 comments on commit 2d72080

Please sign in to comment.