195 lines
6.8 KiB
C#
195 lines
6.8 KiB
C#
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
using OpenArchival.DataAccess;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace OpenArchival.DataAccess;
|
|
|
|
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext<ApplicationUser>(options)
|
|
{
|
|
public DbSet<ArtifactGrouping> ArtifactGroupings { get; set; }
|
|
|
|
public DbSet<ArtifactEntry> ArtifactEntries { get; set; }
|
|
|
|
public DbSet<ArtifactEntryTag> ArtifactEntryTags { get; set; }
|
|
|
|
public DbSet<ArchiveCategory> ArchiveCategories { get; set; }
|
|
|
|
public DbSet<ListedName> ArtifactAssociatedNames { get; set; }
|
|
|
|
public DbSet<FilePathListing> ArtifactFilePaths { get; set; }
|
|
|
|
public DbSet<ArtifactDefect> ArtifactDefects { get; set; }
|
|
|
|
public DbSet<ArtifactStorageLocation> ArtifactStorageLocations { get; set; }
|
|
|
|
public DbSet<ArtifactType> ArtifactTypes { get; set; }
|
|
|
|
public DbSet<ArtifactGroupingViewCount> ArtifactGroupingViewCounts { get; set; }
|
|
|
|
public DbSet<BlogPost> BlogPosts { get; set; }
|
|
|
|
public DbSet<BlogPostTag> BlogPostTags { get; set; }
|
|
|
|
/// <summary>
|
|
/// Configuration for what featured artifacts will be show on the homepage of the search page
|
|
/// </summary>
|
|
public DbSet<SearchPageSliderEntry> SearchPageSliderEntries { get; set; }
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.Entity<ArtifactEntry>()
|
|
.HasMany(a => a.RelatedTo)
|
|
.WithMany(a => a.RelatedBy)
|
|
.UsingEntity(j => j.ToTable("ArtifactRelationships"));
|
|
|
|
modelBuilder.Entity<ArtifactEntry>()
|
|
.HasOne(a => a.StorageLocation)
|
|
.WithMany(l => l.ArtifactEntries);
|
|
|
|
modelBuilder.Entity<ArtifactGroupingViewCount>()
|
|
.HasOne(vc => vc.Grouping)
|
|
.WithOne(g => g.ViewCount)
|
|
.HasForeignKey<ArtifactGroupingViewCount>(vc => vc.ArtifactGroupingId);
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.OwnsOne(p => p.IdentifierFields)
|
|
.ToJson();
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasMany(grouping => grouping.ChildArtifactEntries)
|
|
.WithOne(entry => entry.ArtifactGrouping)
|
|
.HasForeignKey(entry => entry.ArtifactGroupingId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
modelBuilder.Entity<ArtifactEntry>()
|
|
.HasMany(entry => entry.Files)
|
|
.WithOne(file => file.ParentArtifactEntry)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.Navigation(g => g.IdentifierFields)
|
|
.UsePropertyAccessMode(PropertyAccessMode.Field);
|
|
|
|
var dictionaryComparer = new ValueComparer<Dictionary<string, string>>(
|
|
(dictionary1, dictionary2) => dictionary1.OrderBy(pair => pair.Key)
|
|
.SequenceEqual(dictionary2.OrderBy(pair => pair.Key)),
|
|
|
|
dictionary => dictionary.Aggregate(
|
|
0,
|
|
(aggregatedHash, pair) => HashCode.Combine(aggregatedHash, pair.Key.GetHashCode(), pair.Value.GetHashCode())),
|
|
|
|
sourceDictionary => new Dictionary<string, string>(sourceDictionary)
|
|
);
|
|
|
|
// Create the search vector columns for artifat groupings
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.AllSearchVector,
|
|
"english",
|
|
p => p.AllSearchString
|
|
)
|
|
.HasIndex(p => p.AllSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.TagsSearchVector,
|
|
"english",
|
|
p => p.TagsSearchString
|
|
)
|
|
.HasIndex(p => p.TagsSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.DefectsSearchVector,
|
|
"english",
|
|
p => p.DefectsSearchString
|
|
)
|
|
.HasIndex(p => p.DefectsSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.ListedNamesSearchVector,
|
|
"english",
|
|
p => p.ListedNamesSearchString
|
|
)
|
|
.HasIndex(p => p.ListedNamesSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.TitleSearchVector,
|
|
"english",
|
|
p => p.TitleSearchString
|
|
)
|
|
.HasIndex(p => p.TitleSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.DescriptionSearchVector,
|
|
"english",
|
|
p => p.DescriptionSearchString
|
|
)
|
|
.HasIndex(p => p.DescriptionSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.FilenamesSearchVector,
|
|
"english",
|
|
p => p.FilenamesSearchString
|
|
)
|
|
.HasIndex(p => p.FilenamesSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<ArtifactGrouping>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.FileContentSearchVector,
|
|
"english",
|
|
p => p.FileContentSearchString
|
|
)
|
|
.HasIndex(p => p.FileContentSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
// Create the search vector columns for blog posts
|
|
modelBuilder.Entity<BlogPost>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.ContentSearchVector,
|
|
"english",
|
|
p => p.Content)
|
|
.HasIndex(p => p.ContentSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<BlogPost>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.TitleSearchVector,
|
|
"english",
|
|
p => p.Title)
|
|
.HasIndex(p => p.TitleSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<BlogPost>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.TagsSearchVector,
|
|
"english",
|
|
p => p.TagsSearchString)
|
|
.HasIndex(p => p.TagsSearchVector)
|
|
.HasMethod("GIN");
|
|
|
|
modelBuilder.Entity<BlogPost>()
|
|
.HasGeneratedTsVectorColumn(
|
|
p => p.AllSearchVector,
|
|
"english",
|
|
p => p.AllSearchString)
|
|
.HasIndex(p => p.AllSearchVector)
|
|
.HasMethod("GIN");
|
|
}
|
|
}
|