using Microsoft.EntityFrameworkCore; using WyvernInventory.Core.Models; namespace WyvernInventory.Infrastructure.Data; public class DBContext : DbContext { public DBContext(DbContextOptions options) : base(options) {} public DbSet InventoryItems => Set(); public DbSet InventoryAttributeDefinitions => Set(); public DbSet InventoryAttributeValues => Set(); public DbSet InventoryTypes => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasOne(_ => _.Type) .WithMany() .HasForeignKey(_ => _.TypeId); modelBuilder.Entity() .HasOne(_ => _.Item) .WithMany(_ => _.AttributeValues) .HasForeignKey(_ => _.ItemId) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasOne(_ => _.AttributeDefinition) .WithMany() .HasForeignKey(_ => _.AttributeDefinitionId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(_ => _.Type) .WithMany(_ => _.AttributeDefinitions) .HasForeignKey(_ => _.TypeId) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasIndex(_ => new { _.ItemId, _.AttributeDefinitionId}) .IsUnique(); } }