1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

fix memory leak issue related to AddDbContext not working as expected

This commit is contained in:
RaidMax
2020-11-29 16:01:52 -06:00
parent 9ed3e50a63
commit 78ae9dec74
18 changed files with 499 additions and 505 deletions

View File

@ -1,7 +1,8 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database;
using SharedLibraryCore.Database.MigrationContext;
using SharedLibraryCore.Interfaces;
namespace IW4MAdmin.Application.Factories
@ -11,13 +12,15 @@ namespace IW4MAdmin.Application.Factories
/// </summary>
public class DatabaseContextFactory : IDatabaseContextFactory
{
private readonly IServiceProvider _serviceProvider;
public DatabaseContextFactory(IServiceProvider serviceProvider)
private readonly DbContextOptions _contextOptions;
private readonly string _activeProvider;
public DatabaseContextFactory(ApplicationConfiguration appConfig, DbContextOptions contextOptions)
{
_serviceProvider = serviceProvider;
_contextOptions = contextOptions;
_activeProvider = appConfig.DatabaseProvider?.ToLower();
}
/// <summary>
/// creates a new database context
/// </summary>
@ -25,10 +28,10 @@ namespace IW4MAdmin.Application.Factories
/// <returns></returns>
public DatabaseContext CreateContext(bool? enableTracking = true)
{
var context = _serviceProvider.GetRequiredService<DatabaseContext>();
var context = BuildContext();
enableTracking ??= true;
if (enableTracking.Value)
{
context.ChangeTracker.AutoDetectChangesEnabled = true;
@ -44,5 +47,16 @@ namespace IW4MAdmin.Application.Factories
return context;
}
private DatabaseContext BuildContext()
{
return _activeProvider switch
{
"sqlite" => new SqliteDatabaseContext(_contextOptions),
"mysql" => new MySqlDatabaseContext(_contextOptions),
"postgresql" => new PostgresqlDatabaseContext(_contextOptions),
_ => throw new ArgumentException($"No context found for {_activeProvider}")
};
}
}
}
}