JellyfinDbProvider.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Common.Configuration;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.DependencyInjection;
  6. namespace Jellyfin.Server.Implementations
  7. {
  8. /// <summary>
  9. /// Factory class for generating new <see cref="JellyfinDb"/> instances.
  10. /// </summary>
  11. public class JellyfinDbProvider
  12. {
  13. private readonly IServiceProvider _serviceProvider;
  14. private readonly IApplicationPaths _appPaths;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class.
  17. /// </summary>
  18. /// <param name="serviceProvider">The application's service provider.</param>
  19. /// <param name="appPaths">The application paths.</param>
  20. public JellyfinDbProvider(IServiceProvider serviceProvider, IApplicationPaths appPaths)
  21. {
  22. _serviceProvider = serviceProvider;
  23. _appPaths = appPaths;
  24. using var jellyfinDb = CreateContext();
  25. jellyfinDb.Database.Migrate();
  26. }
  27. /// <summary>
  28. /// Creates a new <see cref="JellyfinDb"/> context.
  29. /// </summary>
  30. /// <returns>The newly created context.</returns>
  31. public JellyfinDb CreateContext()
  32. {
  33. var contextOptions = new DbContextOptionsBuilder<JellyfinDb>().UseSqlite($"Filename={Path.Combine(_appPaths.DataPath, "jellyfin.db")}");
  34. return ActivatorUtilities.CreateInstance<JellyfinDb>(_serviceProvider, contextOptions.Options);
  35. }
  36. }
  37. }