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 options) : IdentityDbContext(options) { public DbSet ArtifactGroupings { get; set; } public DbSet ArtifactEntries { get; set; } public DbSet ArtifactEntryTags { get; set; } public DbSet ArchiveCategories { get; set; } public DbSet ArtifactAssociatedNames { get; set; } public DbSet ArtifactFilePaths { get; set; } public DbSet ArtifactDefects { get; set; } public DbSet ArtifactStorageLocations { get; set; } public DbSet ArtifactTypes { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .HasMany(p => p.Files) .WithOne(p => p.ParentArtifactEntry) .IsRequired(false); modelBuilder.Entity() .HasMany(a => a.RelatedTo) .WithMany(a => a.RelatedBy) .UsingEntity(j => j.ToTable("ArtifactRelationships")); modelBuilder.Entity() .OwnsOne(p => p.IdentifierFields) .ToJson(); modelBuilder.Entity() .HasMany(grouping => grouping.ChildArtifactEntries) .WithOne(entry => entry.ArtifactGrouping) .HasForeignKey(entry => entry.ArtifactGroupingId) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .Navigation(g => g.IdentifierFields) .UsePropertyAccessMode(PropertyAccessMode.Field); var dictionaryComparer = new ValueComparer>( (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(sourceDictionary) ); } }