139 lines
5.1 KiB
C#
139 lines
5.1 KiB
C#
using System;
|
|
using GazpromApp.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GazpromApp.Utils;
|
|
|
|
public class AppDbContext : DbContext
|
|
{
|
|
public DbSet<Image> Image { get; set; }
|
|
public DbSet<Equipment> Equipment { get; set; }
|
|
public DbSet<Inspection> Inspection { get; set; }
|
|
public DbSet<Property> Property { get; set; }
|
|
public DbSet<Point> 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<AppDbContext> 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<Point>()
|
|
.HasMany(p => p.Equipment)
|
|
.WithOne(e => e.Point)
|
|
.HasForeignKey(e => e.PointId)
|
|
.OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Equipment.
|
|
|
|
modelBuilder.Entity<Point>()
|
|
.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<Equipment>()
|
|
.HasMany(e => e.Inspections)
|
|
.WithOne(i => i.Equipment)
|
|
.HasForeignKey(i => i.EquipmentId)
|
|
.OnDelete(DeleteBehavior.Cascade); // Deleting Equipment deletes its Inspections.
|
|
|
|
modelBuilder.Entity<Equipment>()
|
|
.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<Point>()
|
|
.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<Equipment>()
|
|
.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<Property>()
|
|
.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<Point>()
|
|
// .Property(p => p.Id)
|
|
// .ValueGeneratedOnAdd();
|
|
// modelBuilder.Entity<Equipment>()
|
|
// .Property(e => e.Id)
|
|
// .ValueGeneratedOnAdd();
|
|
// modelBuilder.Entity<Image>()
|
|
// .Property(i => i.Id)
|
|
// .ValueGeneratedOnAdd();
|
|
// modelBuilder.Entity<Property>()
|
|
// .Property(p => p.Id)
|
|
// .ValueGeneratedOnAdd();
|
|
// modelBuilder.Entity<Inspection>()
|
|
// .Property(i => i.Id)
|
|
// .ValueGeneratedOnAdd();
|
|
//
|
|
// modelBuilder.Entity<Point>()
|
|
// .HasMany(p => p.Images)
|
|
// .WithMany(i => i.Points);
|
|
// modelBuilder.Entity<Point>()
|
|
// .HasMany(p => p.Equipment)
|
|
// .WithOne(e => e.Point)
|
|
// .HasForeignKey(e => e.PointId);
|
|
// modelBuilder.Entity<Point>()
|
|
// .HasMany(p => p.Properties)
|
|
// .WithOne(p => p.Point)
|
|
// .HasForeignKey(p => p.ParentId)
|
|
// .IsRequired(false);
|
|
//
|
|
// modelBuilder.Entity<Equipment>()
|
|
// .HasOne(e => e.Image)
|
|
// .WithMany(i => i.Equipment);
|
|
// modelBuilder.Entity<Equipment>()
|
|
// .HasMany(e => e.Properties)
|
|
// .WithOne(p => p.Equipment)
|
|
// .HasForeignKey(p => p.ParentId)
|
|
// .IsRequired(false);
|
|
// modelBuilder.Entity<Equipment>()
|
|
// .HasMany(e => e.Inspections)
|
|
// .WithOne(i => i.Equipment)
|
|
// .HasForeignKey(i => i.EquipmentId);
|
|
//
|
|
// modelBuilder.Entity<Property>()
|
|
// .HasMany(p => p.Properties)
|
|
// .WithOne(p => p.Parent)
|
|
// .HasForeignKey(p => p.ParentId)
|
|
// .IsRequired(false);
|
|
}
|
|
} |