using System.Text.Json; using Microsoft.EntityFrameworkCore; using SteUp.Shared.Core.Entities; namespace SteUp.Data.LocalDb; public class AppDbContext(DbContextOptions options) : DbContext(options) { public DbSet Ispezioni => Set(); public DbSet Schede => Set(); public DbSet SchedaArticoli => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .HasKey(x => new { x.CodMdep, x.Data, x.Rilevatore }); modelBuilder.Entity() .HasOne(x => x.Ispezione) .WithMany(x => x.Schede) .HasForeignKey(x => new { x.CodMdep, x.Data, x.Rilevatore }) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasIndex(x => new { x.CodMdep, x.Data, x.Rilevatore }); modelBuilder.Entity() .Property(x => x.ImageNames) .HasConversion( v => JsonSerializer.Serialize(v, (JsonSerializerOptions?)null), v => JsonSerializer.Deserialize>(v, (JsonSerializerOptions?)null) ?? new List() ); modelBuilder.Entity() .HasOne(a => a.Scheda) .WithMany(s => s.Articoli) .HasForeignKey(a => a.SchedaId) .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity() .HasIndex(a => new { a.SchedaId, a.Barcode }) .IsUnique(); } }