diff options
| author | cvium <clausvium@gmail.com> | 2020-07-31 10:13:54 +0200 |
|---|---|---|
| committer | cvium <clausvium@gmail.com> | 2020-07-31 10:13:54 +0200 |
| commit | 5f03fb0ef7d25ea36f75feea7577d47a164950c5 (patch) | |
| tree | 6e699938515b3839998afbd7f55f53729193b332 /Jellyfin.Server.Implementations | |
| parent | ee3fae497c14493b8678f126c30580757044d7be (diff) | |
Use factory pattern to instantiate jellyfindb context to avoid disposed contexts piling up in DI container
Diffstat (limited to 'Jellyfin.Server.Implementations')
| -rw-r--r-- | Jellyfin.Server.Implementations/JellyfinDbProvider.cs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/Jellyfin.Server.Implementations/JellyfinDbProvider.cs b/Jellyfin.Server.Implementations/JellyfinDbProvider.cs index 8f5c19900..486be6053 100644 --- a/Jellyfin.Server.Implementations/JellyfinDbProvider.cs +++ b/Jellyfin.Server.Implementations/JellyfinDbProvider.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using MediaBrowser.Common.Configuration; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -10,15 +12,20 @@ namespace Jellyfin.Server.Implementations public class JellyfinDbProvider { private readonly IServiceProvider _serviceProvider; + private readonly IApplicationPaths _appPaths; /// <summary> /// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class. /// </summary> /// <param name="serviceProvider">The application's service provider.</param> - public JellyfinDbProvider(IServiceProvider serviceProvider) + /// <param name="appPaths">The application paths.</param> + public JellyfinDbProvider(IServiceProvider serviceProvider, IApplicationPaths appPaths) { _serviceProvider = serviceProvider; - serviceProvider.GetRequiredService<JellyfinDb>().Database.Migrate(); + _appPaths = appPaths; + + using var jellyfinDb = CreateContext(); + jellyfinDb.Database.Migrate(); } /// <summary> @@ -27,7 +34,8 @@ namespace Jellyfin.Server.Implementations /// <returns>The newly created context.</returns> public JellyfinDb CreateContext() { - return _serviceProvider.GetRequiredService<JellyfinDb>(); + var contextOptions = new DbContextOptionsBuilder<JellyfinDb>().UseSqlite($"Filename={Path.Combine(_appPaths.DataPath, "jellyfin.db")}"); + return ActivatorUtilities.CreateInstance<JellyfinDb>(_serviceProvider, contextOptions.Options); } } } |
