using System; using GazpromApp.Models; using Microsoft.EntityFrameworkCore; namespace GazpromApp.Utils; public class AppDbContext : DbContext { public DbSet Image { get; set; } public DbSet Equipment { get; set; } public DbSet Inspection { get; set; } public DbSet Property { get; set; } public DbSet Point { get; set; } public string DbPath { get; } public AppDbContext() { var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); DbPath = System.IO.Path.Combine(path, "GazpromApp.db"); } public AppDbContext(DbContextOptions options) : base(options) { var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); DbPath = System.IO.Path.Combine(path, "GazpromApp.db"); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.UseSqlite($"Data Source={DbPath}"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // ==== Point RELATIONSHIPS ==== modelBuilder.Entity() .HasMany(p => p.Equipment) .WithOne(e => e.Point) .HasForeignKey(e => e.PointId) .OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Equipment. modelBuilder.Entity() .HasMany(p => p.Images) .WithOne(i => i.Point) .HasForeignKey(i => i.PointId) .OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Images. // ==== Equipment RELATIONSHIPS ==== modelBuilder.Entity() .HasMany(e => e.Inspections) .WithOne(i => i.Equipment) .HasForeignKey(i => i.EquipmentId) .OnDelete(DeleteBehavior.Cascade); // Deleting Equipment deletes its Inspections. modelBuilder.Entity() .HasMany(e => e.Images) .WithOne(i => i.Equipment) .HasForeignKey(i => i.EquipmentId) .OnDelete(DeleteBehavior.Cascade); // Equipment has one Image. // ==== PROPERTY RELATIONSHIPS ==== // Property as a child of a Point modelBuilder.Entity() .HasMany(p => p.Properties) .WithOne(prop => prop.Point) .HasForeignKey(prop => prop.PointId) .IsRequired(false) .OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Properties. // Property as a child of an Equipment modelBuilder.Entity() .HasMany(e => e.Properties) .WithOne(prop => prop.Equipment) .HasForeignKey(prop => prop.EquipmentId) .IsRequired(false) .OnDelete(DeleteBehavior.Cascade); // Deleting Equipment deletes its Properties. // Property as a child of another Property (self-referencing) modelBuilder.Entity() .HasMany(p => p.Properties) .WithOne(p => p.Parent) .HasForeignKey(p => p.ParentPropertyId) .IsRequired(false) .OnDelete(DeleteBehavior.Cascade); // Deleting a parent Property deletes its children. // modelBuilder.Entity() // .Property(p => p.Id) // .ValueGeneratedOnAdd(); // modelBuilder.Entity() // .Property(e => e.Id) // .ValueGeneratedOnAdd(); // modelBuilder.Entity() // .Property(i => i.Id) // .ValueGeneratedOnAdd(); // modelBuilder.Entity() // .Property(p => p.Id) // .ValueGeneratedOnAdd(); // modelBuilder.Entity() // .Property(i => i.Id) // .ValueGeneratedOnAdd(); // // modelBuilder.Entity() // .HasMany(p => p.Images) // .WithMany(i => i.Points); // modelBuilder.Entity() // .HasMany(p => p.Equipment) // .WithOne(e => e.Point) // .HasForeignKey(e => e.PointId); // modelBuilder.Entity() // .HasMany(p => p.Properties) // .WithOne(p => p.Point) // .HasForeignKey(p => p.ParentId) // .IsRequired(false); // // modelBuilder.Entity() // .HasOne(e => e.Image) // .WithMany(i => i.Equipment); // modelBuilder.Entity() // .HasMany(e => e.Properties) // .WithOne(p => p.Equipment) // .HasForeignKey(p => p.ParentId) // .IsRequired(false); // modelBuilder.Entity() // .HasMany(e => e.Inspections) // .WithOne(i => i.Equipment) // .HasForeignKey(i => i.EquipmentId); // // modelBuilder.Entity() // .HasMany(p => p.Properties) // .WithOne(p => p.Parent) // .HasForeignKey(p => p.ParentId) // .IsRequired(false); } }