#region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.DataAccess.Entities.Authorization; using Lumina.Application.Common.DataAccess.Entities.Favorites; using Lumina.Application.Common.DataAccess.Entities.MediaConsumption; using Lumina.Application.Common.DataAccess.Entities.MediaContributors; using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management; using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary; using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary; using Lumina.Application.Common.DataAccess.Entities.Playlists; using Lumina.Application.Common.DataAccess.Entities.Plugins; using Lumina.Application.Common.DataAccess.Entities.Scheduling; using Lumina.Application.Common.DataAccess.Entities.Themes; using Lumina.Application.Common.DataAccess.Entities.UsersManagement; using Lumina.DataAccess.Common.ValueGeneration; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using System; #endregion namespace Lumina.DataAccess.Core.UoW; /// /// DbContext for the Lumina application. /// /// To add a new migration: add-migration InitialMigration -p Lumina.DataAccess -s Lumina.Presentation.Api -o Common\Migrations /// To update the database: update-database -p Lumina.DataAccess -s Lumina.Presentation.Api public class LuminaDbContext : DbContext { /// /// Gets or sets the books stored in the database. /// public virtual DbSet Books { get; set; } = null!; /// /// Gets or sets the artists stored in the database. /// public virtual DbSet Artists { get; set; } = null!; /// /// Gets or sets the albums stored in the database. /// public virtual DbSet Albums { get; set; } = null!; /// /// Gets or sets the tracks stored in the database. /// public virtual DbSet Tracks { get; set; } = null!; /// /// Gets or sets the artwork of the music library items stored in the database. /// public virtual DbSet MusicArtwork { get; set; } = null!; /// /// Gets or sets the metadata extracted from the music files discovered during a scan, stored in the database. /// public virtual DbSet MusicLibraryScanItemMetadata { get; set; } = null!; /// /// Gets or sets the users stored in the database. /// public virtual DbSet Users { get; set; } = null!; /// /// Gets or sets the settings of the users stored in the database. /// public virtual DbSet UserSettings { get; set; } = null!; /// /// Gets or sets the media libraries stored in the database. /// public virtual DbSet Libraries { get; set; } = null!; /// /// Gets or sets the scans of the media libraries stored in the database. /// public virtual DbSet LibraryScans { get; set; } = null!; /// /// Gets or sets the results of the media library scans stored in the database. /// public virtual DbSet LibraryScanResults { get; set; } = null!; /// /// Gets or sets the snapshots of the media library items produced by the scans, stored in the database. /// public virtual DbSet LibraryScanSnapshots { get; set; } = null!; /// /// Gets or sets the staging results of the media library scans, holding the discovered file system items, stored in the database. /// public virtual DbSet LibraryScanStagingResults { get; set; } = null!; /// /// Gets or sets the fingerprints of the scanned directories, holding their last write time, stored in the database. /// public virtual DbSet DirectoryScanFingerprints { get; set; } = null!; /// /// Gets or sets the authorization roles stored in the database. /// public virtual DbSet Roles { get; set; } = null!; /// /// Gets or sets the associations between users and roles stored in the database. /// public virtual DbSet UserRoles { get; set; } = null!; /// /// Gets or sets the authorization permissions stored in the database. /// public virtual DbSet Permissions { get; set; } = null!; /// /// Gets or sets the associations between users and permissions stored in the database. /// public virtual DbSet UserPermissions { get; set; } = null!; /// /// Gets or sets the associations between roles and permissions stored in the database. /// public virtual DbSet RolePermissions { get; set; } = null!; /// /// Gets or sets the plugins stored in the database. /// public virtual DbSet Plugins { get; set; } = null!; /// /// Gets or sets the metadata provider configurations of the media libraries stored in the database. /// public virtual DbSet LibraryMetadataProviderConfigurations { get; set; } = null!; /// /// Gets or sets the artwork provider configurations of the media libraries stored in the database. /// public virtual DbSet LibraryArtworkProviderConfigurations { get; set; } = null!; /// /// Gets or sets the book reader configurations of the media libraries stored in the database. /// public virtual DbSet LibraryBookReaderConfigurations { get; set; } = null!; /// /// Gets or sets the themes stored in the database. /// public virtual DbSet Themes { get; set; } = null!; /// /// Gets or sets the media contributors stored in the database. /// public virtual DbSet MediaContributors { get; set; } = null!; /// /// Gets or sets the participations of the media contributors in books stored in the database. /// public virtual DbSet BookContributors { get; set; } = null!; /// /// Gets or sets the scheduled jobs stored in the database. /// public virtual DbSet ScheduledJobs { get; set; } = null!; /// /// Gets or sets the executions of the scheduled jobs stored in the database. /// public virtual DbSet ScheduledJobExecutions { get; set; } = null!; /// /// Gets or sets the display preferences of the scheduler page of the users stored in the database. /// public virtual DbSet SchedulerDisplayPreferences { get; set; } = null!; /// /// Gets or sets the playlists stored in the database. /// public virtual DbSet Playlists { get; set; } = null!; /// /// Gets or sets the entries of the playlists stored in the database. /// public virtual DbSet PlaylistEntries { get; set; } = null!; /// /// Gets or sets the favorites stored in the database. /// public virtual DbSet Favorites { get; set; } = null!; /// /// Gets or sets the media consumption records stored in the database. /// public virtual DbSet MediaConsumptionRecords { get; set; } = null!; /// /// Gets or sets the media resume points stored in the database. /// public virtual DbSet MediaResumePoints { get; set; } = null!; /// /// Initializes a new instance of the class. /// /// The options for configuring the database context. public LuminaDbContext(DbContextOptions options) : base(options) { } /// /// Configures the entity mappings for the database context. /// /// The model builder used to configure the entity mappings. protected override void OnModelCreating(ModelBuilder modelBuilder) { // Find all the entity configurations in the assembly and apply them. modelBuilder.ApplyConfigurationsFromAssembly(typeof(LuminaDbContext).Assembly); // The relational provider seeds its Guid value generators from Guid.NewGuid, so replace them with a time ordered generator for every // identifier that the persistence layer assigns itself. This covers real keys, such as PermissionEntity.Id, and the shadow keys of // owned collections, such as AlbumReleaseTypes.Id. foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes()) foreach (IMutableProperty property in entityType.GetProperties()) if (property.ClrType == typeof(Guid) && property.ValueGenerated == ValueGenerated.OnAdd) property.SetValueGeneratorFactory((generatedProperty, declaringType) => new GuidV7ValueGenerator()); base.OnModelCreating(modelBuilder); } }