Files
MilkyShots/Lactose/Context/LactoseDbContext.cs
T

116 lines
5.0 KiB
C#

using Lactose.Models;
namespace Lactose.Context;
/// <summary>
/// Entity Framework Core database context for the Lactose application.
/// </summary>
public class LactoseDbContext : DbContext {
/// <summary>
/// Gets or sets the assets DbSet.
/// </summary>
public DbSet<Asset> Assets { get; set; }
/// <summary>
/// Gets or sets the albums DbSet.
/// </summary>
public DbSet<Album> Albums { get; set; }
/// <summary>
/// Gets or sets the faces DbSet.
/// </summary>
public DbSet<Face> Faces { get; set; }
/// <summary>
/// Gets or sets the people DbSet.
/// </summary>
public DbSet<Person> People { get; set; }
/// <summary>
/// Gets or sets the users DbSet.
/// </summary>
public DbSet<User> Users { get; set; }
/// <summary>
/// Gets or sets the tags DbSet.
/// </summary>
public DbSet<Tag> Tags { get; set; }
/// <summary>
/// Gets or sets the folders DbSet.
/// </summary>
public DbSet<Folder> Folders { get; set; }
/// <summary>
/// Gets or sets the settings DbSet.
/// </summary>
public DbSet<Setting> Settings { get; set; }
/// <summary>
/// Gets or sets the job records DbSet.
/// </summary>
public DbSet<JobRecord> JobRecords { get; set; }
/// <summary>
/// Gets or sets the person-maintainer join table.
/// </summary>
public DbSet<PersonMaintainer> PersonMaintainers { get; set; }
/// <summary>
/// Initialises a new instance of the <see cref="LactoseDbContext"/> class.
/// </summary>
/// <param name="options">The database context options.</param>
public LactoseDbContext(DbContextOptions options) : base(options) {}
/// <inheritdoc />
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) {
configurationBuilder.Properties<DateTime>()
.HaveColumnType("timestamp with time zone");
configurationBuilder.Properties<DateTime?>()
.HaveColumnType("timestamp with time zone");
}
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.HasPostgresExtension("vector");
modelBuilder.HasPostgresExtension("pg_trgm");
//Album Relationships
modelBuilder.Entity<Album>().HasOne(e => e.PersonOwner).WithMany(e => e.Albums);
modelBuilder.Entity<Album>().HasOne(e => e.CoverAsset);
modelBuilder.Entity<Album>().HasMany(e => e.Assets).WithMany(e => e.Albums);
//Asset Relationships
modelBuilder.Entity<Asset>().HasOne(e => e.Uploader).WithMany(e => e.UploadedAssets).HasForeignKey(e => e.UploadedBy);
modelBuilder.Entity<Asset>().HasMany(e => e.Albums).WithMany(e => e.Assets);
modelBuilder.Entity<Asset>().HasMany(e => e.Tags).WithMany(e => e.Assets);
modelBuilder.Entity<Asset>().HasMany(e => e.Faces).WithOne(e => e.Asset);
modelBuilder.Entity<Asset>().HasOne(e => e.Folder).WithMany(e => e.Assets);
modelBuilder.Entity<Asset>().HasIndex(e => new { e.Visibility, e.UploadedBy })
.HasDatabaseName("IX_Assets_VisibleForSearch")
.HasFilter("\"DeletedAt\" IS NULL");
modelBuilder.Entity<Asset>().HasIndex(a => a.OriginalFilename)
.HasDatabaseName("IX_Assets_OriginalFilename_Trgm")
.HasMethod("gin")
.HasOperators("gin_trgm_ops");
// People
modelBuilder.Entity<Person>().HasIndex(p => p.Name)
.HasDatabaseName("IX_People_Name_Trgm")
.HasMethod("gin")
.HasOperators("gin_trgm_ops");
// Albums
modelBuilder.Entity<Album>().HasIndex(a => a.Title)
.HasDatabaseName("IX_Albums_Title_Trgm")
.HasMethod("gin")
.HasOperators("gin_trgm_ops");
//Face Relationships
modelBuilder.Entity<Face>().HasOne(e => e.Asset).WithMany(e => e.Faces);
modelBuilder.Entity<Face>().HasOne(e => e.Person).WithMany(e => e.Faces);
//Person Relationships
modelBuilder.Entity<Person>().HasOne(e => e.ProfileAsset).WithMany().HasForeignKey(e => e.ProfileAssetId);
modelBuilder.Entity<Person>().HasMany(e => e.Faces).WithOne(e => e.Person);
modelBuilder.Entity<Person>().HasMany(e => e.Albums).WithOne(e => e.PersonOwner);
modelBuilder.Entity<Person>().HasMany(e => e.Maintainers).WithMany(e => e.MaintainedPersons)
.UsingEntity<PersonMaintainer>(
j => j.HasOne(pm => pm.User).WithMany().HasForeignKey(pm => pm.UserId),
j => j.HasOne(pm => pm.Person).WithMany().HasForeignKey(pm => pm.PersonId)
);
//Tag Relationships
modelBuilder.Entity<Tag>().HasOne(e => e.Parent);
modelBuilder.Entity<Tag>().HasMany(e => e.Assets).WithMany(e => e.Tags);
//User Relationships
modelBuilder.Entity<User>().HasMany(e => e.UploadedAssets).WithOne(e => e.Uploader).HasForeignKey(e => e.UploadedBy);
//Folder Relationships
modelBuilder.Entity<Folder>().HasMany(e => e.Assets).WithOne(e => e.Folder);
}
}