using System; using System.IO; 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); Directory.CreateDirectory(Path.Combine(path, "GazpromApp")); DbPath = Path.Combine(Path.Combine(path, "GazpromApp"), "GazpromApp.db"); } public AppDbContext(DbContextOptions options) : base(options) { var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); Directory.CreateDirectory(Path.Combine(path, "GazpromApp")); DbPath = Path.Combine(Path.Combine(path, "GazpromApp"), "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) .IsRequired(false) .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. modelBuilder.Entity() .HasMany(e => e.Equipments) .WithOne(e => e.ParentEquipment) .HasForeignKey(e => e.ParentEquipmentId) .IsRequired(false) .OnDelete(DeleteBehavior.Cascade); // Equipment has many Equipments. // ==== 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. } }