// Copyright (C) 2024 Intro-Skipper contributors // SPDX-License-Identifier: GPL-3.0-only. using System; using Microsoft.EntityFrameworkCore; namespace IntroSkipper.Db; /// /// Plugin database. /// /// /// Initializes a new instance of the class. /// /// The path to the SQLite database file. public class IntroSkipperDbContext(string dbPath) : DbContext { private readonly string _dbPath = dbPath ?? throw new ArgumentNullException(nameof(dbPath)); /// /// Gets or sets the containing the segments. /// public DbSet DbSegment { get; set; } = null!; /// /// Gets or sets the containing the season information. /// public DbSet DbSeasonInfo { get; set; } = null!; /// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite($"Data Source={_dbPath}") .EnableSensitiveDataLogging(false); } /// protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => { entity.ToTable("DbSegment"); entity.HasKey(s => new { s.ItemId, s.Type }); entity.Property(e => e.ItemId) .IsRequired(); entity.Property(e => e.Type) .IsRequired(); entity.Property(e => e.Start); entity.Property(e => e.End); }); modelBuilder.Entity(entity => { entity.ToTable("DbSeasonInfo"); entity.HasKey(s => new { s.SeasonId, s.Type }); entity.Property(e => e.SeasonId) .IsRequired(); entity.Property(e => e.Type) .IsRequired(); entity.Property(e => e.Action); }); base.OnModelCreating(modelBuilder); } /// /// Applies any pending migrations to the database. /// public void ApplyMigrations() { Database.Migrate(); } }