Setup DbContext
This commit is contained in:
@@ -0,0 +1,171 @@
|
|||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using OakArchive.Entities.CardMarket;
|
||||||
|
using OakArchive.Entities.Cards;
|
||||||
|
using OakArchive.Entities.Collection;
|
||||||
|
using OakArchive.Entities.Grading;
|
||||||
|
using OakArchive.Entities.Serie;
|
||||||
|
using OakArchive.Entities.Set;
|
||||||
|
using OakArchive.Entities.TCGdex;
|
||||||
|
using OakArchive.Entities.User;
|
||||||
|
|
||||||
|
namespace OakArchive.Database;
|
||||||
|
|
||||||
|
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>
|
||||||
|
{
|
||||||
|
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<Settings> Settings { get; set; } = null!;
|
||||||
|
public DbSet<Collection> Collections { get; set; } = null!;
|
||||||
|
public DbSet<CollectedCard> CollectedCards { get; set; } = null!;
|
||||||
|
public DbSet<Grading> Gradings { get; set; } = null!;
|
||||||
|
public DbSet<TcgDexCardInfo> TcgDexCardInfos { get; set; } = null!;
|
||||||
|
public DbSet<TcgDexSetInfo> TcgDexSetInfos { get; set; } = null!;
|
||||||
|
public DbSet<TcgDexSeriesInfo> TcgDexSeriesInfos { get; set; } = null!;
|
||||||
|
public DbSet<Serie> Series { get; set; } = null!;
|
||||||
|
public DbSet<Set> Sets { get; set; } = null!;
|
||||||
|
public DbSet<Legal> Legals { get; set; } = null!;
|
||||||
|
public DbSet<CardCount> CardCounts { get; set; } = null!;
|
||||||
|
public DbSet<Card> Cards { get; set; } = null!;
|
||||||
|
public DbSet<Attack> Attacks { get; set; } = null!;
|
||||||
|
public DbSet<Weakness> Weaknesses { get; set; } = null!;
|
||||||
|
public DbSet<CardMarketSetInfo> CardMarketSetInfos { get; set; } = null!;
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
// 1. Basis-Mapping für ASP.NET Core Identity (Pflicht!)
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
|
||||||
|
#region User & Settings
|
||||||
|
|
||||||
|
modelBuilder.Entity<ApplicationUser>(entity =>
|
||||||
|
{
|
||||||
|
// Konfiguration für das private Feld 'DefaultCollection' als Backing Field
|
||||||
|
entity.Property<Guid>("DefaultCollectionId");
|
||||||
|
|
||||||
|
entity.HasOne<Collection>()
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("DefaultCollectionId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict);
|
||||||
|
|
||||||
|
// n:m Beziehung zwischen Usern und Collections (Members)
|
||||||
|
entity.HasMany(u => u.Collections)
|
||||||
|
.WithMany(c => c.Members)
|
||||||
|
.UsingEntity(j => j.ToTable("UserCollections"));
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<Settings>()
|
||||||
|
.HasOne(s => s.User)
|
||||||
|
.WithOne(u => u.Settings)
|
||||||
|
.HasForeignKey<Settings>(s => s.UserId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 1:1 TCGdex & CardMarket Mapping
|
||||||
|
|
||||||
|
// EF Core mitgeteilt, auf welcher Seite der FK physisch liegt
|
||||||
|
modelBuilder.Entity<Card>()
|
||||||
|
.HasOne(c => c.TcgDexCardInfo)
|
||||||
|
.WithOne(t => t.Card)
|
||||||
|
.HasForeignKey<TcgDexCardInfo>(t => t.CardId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Set>()
|
||||||
|
.HasOne(s => s.SetInfo)
|
||||||
|
.WithOne(t => t.Set)
|
||||||
|
.HasForeignKey<TcgDexSetInfo>(t => t.SetIdRef)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Serie>()
|
||||||
|
.HasOne(s => s.TcgDexSeriesInfo)
|
||||||
|
.WithOne(t => t.Series)
|
||||||
|
.HasForeignKey<TcgDexSeriesInfo>(t => t.SeriesIdRef)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Set>()
|
||||||
|
.HasOne(s => s.CardCount)
|
||||||
|
.WithOne(c => c.Set)
|
||||||
|
.HasForeignKey<CardCount>(c => c.SetId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
modelBuilder.Entity<Set>()
|
||||||
|
.HasOne(s => s.CardMarketSetInfo)
|
||||||
|
.WithOne(cms => cms.Set)
|
||||||
|
.HasForeignKey<CardMarketSetInfo>(cms => cms.SetId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
// Auflösung der geteilten Legal-Tabelle
|
||||||
|
modelBuilder.Entity<Legal>(entity =>
|
||||||
|
{
|
||||||
|
entity.HasOne(l => l.Set)
|
||||||
|
.WithOne(s => s.Legal)
|
||||||
|
.HasForeignKey<Legal>(l => l.SetId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
entity.HasOne(l => l.Card)
|
||||||
|
.WithOne(c => c.Legal)
|
||||||
|
.HasForeignKey<Legal>(l => l.CardId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Postgres JSONB Columns
|
||||||
|
|
||||||
|
modelBuilder.Entity<Card>(entity =>
|
||||||
|
{
|
||||||
|
entity.HasOne(c => c.Set)
|
||||||
|
.WithMany(s => s.Cards)
|
||||||
|
.HasForeignKey(c => c.SetId)
|
||||||
|
.OnDelete(DeleteBehavior.Restrict);
|
||||||
|
|
||||||
|
// Native .NET Listen als JSONB in Postgres speichern
|
||||||
|
entity.Property(c => c.Variants).HasColumnType("jsonb");
|
||||||
|
entity.Property(c => c.Types).HasColumnType("jsonb");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<Attack>(entity =>
|
||||||
|
{
|
||||||
|
entity.HasOne(a => a.Card)
|
||||||
|
.WithMany(c => c.Attacks)
|
||||||
|
.HasForeignKey(a => a.CardId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
entity.Property(a => a.Cost).HasColumnType("jsonb");
|
||||||
|
});
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region CollectedCard & Grading
|
||||||
|
|
||||||
|
modelBuilder.Entity<CollectedCard>(entity =>
|
||||||
|
{
|
||||||
|
entity.HasOne(cc => cc.Collection)
|
||||||
|
.WithMany(c => c.Cards)
|
||||||
|
.HasForeignKey(cc => cc.CollectionId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
entity.HasOne(cc => cc.Card)
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey(cc => cc.CardId)
|
||||||
|
.OnDelete(DeleteBehavior.Restrict);
|
||||||
|
|
||||||
|
entity.HasOne(cc => cc.ScannedBy)
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey(cc => cc.ScannedById)
|
||||||
|
.OnDelete(DeleteBehavior.Restrict);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<Grading>()
|
||||||
|
.HasOne(g => g.CollectedCard)
|
||||||
|
.WithOne(cc => cc.Grading)
|
||||||
|
.HasForeignKey<Grading>(g => g.CollectedCardId)
|
||||||
|
.OnDelete(DeleteBehavior.Cascade);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user