DB integration added, multiple images for equipment
This commit is contained in:
+114
-31
@@ -1,4 +1,5 @@
|
||||
using GazpromApp.Models;
|
||||
using System;
|
||||
using GazpromApp.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GazpromApp.Utils;
|
||||
@@ -7,50 +8,132 @@ 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=MainDB.db");
|
||||
optionsBuilder.UseSqlite($"Data Source={DbPath}");
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Images
|
||||
|
||||
// ==== Point RELATIONSHIPS ====
|
||||
modelBuilder.Entity<Point>()
|
||||
.HasMany(e => e.Images)
|
||||
.WithMany(e => e.Points);
|
||||
.HasMany(p => p.Equipment)
|
||||
.WithOne(e => e.Point)
|
||||
.HasForeignKey(e => e.PointId)
|
||||
.OnDelete(DeleteBehavior.Cascade); // Deleting a Point deletes its Equipment.
|
||||
|
||||
modelBuilder.Entity<Equipment>()
|
||||
.HasOne(e => e.Image)
|
||||
.WithMany(e => e.Equipment)
|
||||
.HasForeignKey(e => e.ImageId);
|
||||
|
||||
// Properties
|
||||
modelBuilder.Entity<Point>()
|
||||
.HasMany(e => e.Properties)
|
||||
.WithMany(e => e.Points);
|
||||
|
||||
modelBuilder.Entity<Equipment>()
|
||||
.HasMany(e => e.Properties)
|
||||
.WithMany(e => e.Equipment);
|
||||
|
||||
modelBuilder.Entity<Property>()
|
||||
.HasMany(e => e.Properties)
|
||||
.WithOne(e => e.Parent)
|
||||
.HasForeignKey(e => e.ParentId);
|
||||
|
||||
// Equipment
|
||||
modelBuilder.Entity<Equipment>()
|
||||
.HasOne(e => e.Point)
|
||||
.WithMany(e => e.Equipment)
|
||||
.HasForeignKey(e => e.PointId);
|
||||
.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(e => e.Equipment)
|
||||
.HasForeignKey(e => e.EquipmentId);
|
||||
.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user