From 2899be8c779d7bd985cac6a05f81363adafdcd7c Mon Sep 17 00:00:00 2001 From: Ammar Hammad Date: Sun, 29 Dec 2024 17:24:23 +0200 Subject: [PATCH 1/2] push Edit.cshtml, Index.cshtml --- ProtoSCADA.MVC/Views/Machine/Edit.cshtml | 42 +++++++++++++++++++++-- ProtoSCADA.MVC/Views/Machine/Index.cshtml | 23 +++++++------ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/ProtoSCADA.MVC/Views/Machine/Edit.cshtml b/ProtoSCADA.MVC/Views/Machine/Edit.cshtml index e1dd794..84b7665 100644 --- a/ProtoSCADA.MVC/Views/Machine/Edit.cshtml +++ b/ProtoSCADA.MVC/Views/Machine/Edit.cshtml @@ -1,5 +1,41 @@ -@* - For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -*@ +@model ProtoSCADA.Entities.DTOs.MachineDto + @{ + ViewData["Title"] = "Edit Machine"; } + +
+

Edit Machine

+
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + Cancel +
+
+
diff --git a/ProtoSCADA.MVC/Views/Machine/Index.cshtml b/ProtoSCADA.MVC/Views/Machine/Index.cshtml index 8cd6941..f2f32ad 100644 --- a/ProtoSCADA.MVC/Views/Machine/Index.cshtml +++ b/ProtoSCADA.MVC/Views/Machine/Index.cshtml @@ -19,17 +19,18 @@ @foreach (var machine in Model) { - - @machine.MachineID - @machine.MachineType - @machine.Status - @machine.LastMaintance.ToString("yyyy-MM-dd") - @machine.FactorName - - View Details - Delete - - + + @machine.MachineID + @machine.MachineType + @machine.Status + @machine.LastMaintance.ToString("yyyy-MM-dd") + @machine.FactorName + + View Details + Edit + Delete + + } From 66e48149f34b02f8b1d1d9caa40d822f7b7861bd Mon Sep 17 00:00:00 2001 From: Ammar Hammad Date: Mon, 30 Dec 2024 16:26:39 +0200 Subject: [PATCH 2/2] Edit table columns, add new tables, and update relationships Description: Updated existing table columns and their data. Added new tables: Report, Line, Role, and populated them with extensive data. Refined and adjusted relationships between the tables to ensure consistency and integrity. --- .../Controllers/MachineController.cs | 2 +- ProtoSCADA.Api/Controllers/UserController.cs | 4 +- .../Context/ApplicationDbContext.cs | 173 +++- ...ddLineAndReportTablesToProject.Designer.cs | 482 ++++++++++ ...0090714_AddLineAndReportTablesToProject.cs | 210 +++++ ...6_UpdateRelationshipsAndSchema.Designer.cs | 591 ++++++++++++ ...1230122026_UpdateRelationshipsAndSchema.cs | 847 ++++++++++++++++++ .../ApplicationDbContextModelSnapshot.cs | 334 ++++++- ProtoSCADA.Entities/Entities/Alert.cs | 26 +- .../Entities/Base/BaseEntity.cs | 17 + ProtoSCADA.Entities/Entities/Event.cs | 23 +- ProtoSCADA.Entities/Entities/Factory.cs | 25 +- ProtoSCADA.Entities/Entities/Line.cs | 28 + ProtoSCADA.Entities/Entities/Machine.cs | 30 +- ProtoSCADA.Entities/Entities/Metric.cs | 22 +- ProtoSCADA.Entities/Entities/Report.cs | 29 + ProtoSCADA.Entities/Entities/Role.cs | 12 + ProtoSCADA.Entities/Entities/User.cs | 30 +- 18 files changed, 2760 insertions(+), 125 deletions(-) create mode 100644 ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.Designer.cs create mode 100644 ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.cs create mode 100644 ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.Designer.cs create mode 100644 ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.cs create mode 100644 ProtoSCADA.Entities/Entities/Base/BaseEntity.cs create mode 100644 ProtoSCADA.Entities/Entities/Line.cs create mode 100644 ProtoSCADA.Entities/Entities/Report.cs create mode 100644 ProtoSCADA.Entities/Entities/Role.cs diff --git a/ProtoSCADA.Api/Controllers/MachineController.cs b/ProtoSCADA.Api/Controllers/MachineController.cs index 3ad3b2f..c62a173 100644 --- a/ProtoSCADA.Api/Controllers/MachineController.cs +++ b/ProtoSCADA.Api/Controllers/MachineController.cs @@ -123,7 +123,7 @@ private static MachineDto MapToDto(Machine machine) MachineID = machine.ID, MachineType = machine.Type, Status = machine.Status, - LastMaintance = machine.LastMaintance, + LastMaintance = machine.LastMaintenance, FactorName = machine.Factory?.Name // Safely access Factory properties }; } diff --git a/ProtoSCADA.Api/Controllers/UserController.cs b/ProtoSCADA.Api/Controllers/UserController.cs index 8704fc3..bb499d6 100644 --- a/ProtoSCADA.Api/Controllers/UserController.cs +++ b/ProtoSCADA.Api/Controllers/UserController.cs @@ -39,7 +39,7 @@ public async Task>>> GetAllUsers ID = user.ID, Name = user.Name, Email = user.Email, - Role = user.Role, + Role = user.Role.RoleName, CreatedAt = user.CreatedAt }); @@ -72,7 +72,7 @@ public async Task> GetUserByID(int id) ID = result.Data.ID, Name = result.Data.Name, Email = result.Data.Email, - Role = result.Data.Role, + Role = result.Data.Role.RoleName, CreatedAt = result.Data.CreatedAt }; return Ok(ProcessResult.Success(result.Message, userDto)); diff --git a/ProtoSCADA.DataService/Context/ApplicationDbContext.cs b/ProtoSCADA.DataService/Context/ApplicationDbContext.cs index 5354b3a..f7bbbdd 100644 --- a/ProtoSCADA.DataService/Context/ApplicationDbContext.cs +++ b/ProtoSCADA.DataService/Context/ApplicationDbContext.cs @@ -22,10 +22,13 @@ public ApplicationDbContext(DbContextOptions options) : ba public DbSet Alerts { get; set; } public DbSet Events { get; set; } - public DbSet Factorys { get; set; } + public DbSet Factories { get; set; } public DbSet Machines { get; set; } public DbSet Metrics { get; set; } public DbSet Users { get; set; } + public DbSet Lines { get; set; } + public DbSet Reports { get; set; } + public DbSet Roles { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) @@ -34,6 +37,174 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + // User and Role + modelBuilder.Entity() + .HasOne(u => u.Role) + .WithMany() + .HasForeignKey(u => u.RoleID) + .OnDelete(DeleteBehavior.Restrict); + + // User and Factory + modelBuilder.Entity() + .HasOne(u => u.Factory) + .WithMany(f => f.Users) + .HasForeignKey(u => u.FactoryID) + .OnDelete(DeleteBehavior.Restrict); + + // User and Line + modelBuilder.Entity() + .HasOne(u => u.Line) + .WithMany() + .HasForeignKey(u => u.LineID) + .OnDelete(DeleteBehavior.Restrict); + + // Factory and Machines + modelBuilder.Entity() + .HasOne(m => m.Factory) + .WithMany(f => f.Machines) + .HasForeignKey(m => m.FactoryID) + .OnDelete(DeleteBehavior.Restrict); + + // Factory and Lines + modelBuilder.Entity() + .HasOne(l => l.Factory) + .WithMany(f => f.Lines) + .HasForeignKey(l => l.FactoryID) + .OnDelete(DeleteBehavior.Restrict); + + // Factory and Reports + modelBuilder.Entity() + .HasOne(r => r.Factory) + .WithMany(f => f.Reports) + .HasForeignKey(r => r.FactoryID) + .OnDelete(DeleteBehavior.Restrict); + + // Factory and Alerts + modelBuilder.Entity() + .HasOne(a => a.Factory) + .WithMany(f => f.Alerts) + .HasForeignKey(a => a.FactoryID) + .OnDelete(DeleteBehavior.Restrict); + + // Factory and Events + modelBuilder.Entity() + .HasOne(e => e.Factory) + .WithMany(f => f.Events) + .HasForeignKey(e => e.FactoryID) + .OnDelete(DeleteBehavior.Restrict); + + // Line and Machines + modelBuilder.Entity() + .HasOne(m => m.Line) + .WithMany(l => l.Machines) + .HasForeignKey(m => m.LineID) + .OnDelete(DeleteBehavior.Restrict); + + // Line and Supervisor (User) + modelBuilder.Entity() + .HasOne(l => l.Supervisor) + .WithMany() + .HasForeignKey(l => l.SupervisorID) + .OnDelete(DeleteBehavior.Restrict); + + // Line and Alerts + modelBuilder.Entity() + .HasOne(a => a.Line) + .WithMany(l => l.Alerts) + .HasForeignKey(a => a.LineID) + .OnDelete(DeleteBehavior.Restrict); + + // Line and Events + modelBuilder.Entity() + .HasOne(e => e.Line) + .WithMany(l => l.Events) + .HasForeignKey(e => e.LineID) + .OnDelete(DeleteBehavior.Restrict); + + // Line and Reports + modelBuilder.Entity() + .HasOne(r => r.Line) + .WithMany(l => l.Reports) + .HasForeignKey(r => r.LineID) + .OnDelete(DeleteBehavior.Restrict); + + // Machine and Metrics + modelBuilder.Entity() + .HasOne(m => m.Machine) + .WithMany(ma => ma.Metrics) + .HasForeignKey(m => m.MachineID) + .OnDelete(DeleteBehavior.Restrict); + + // Machine and Alerts + modelBuilder.Entity() + .HasOne(a => a.Machine) + .WithMany(m => m.Alerts) + .HasForeignKey(a => a.MachineID) + .OnDelete(DeleteBehavior.Restrict); + + // Machine and Events + modelBuilder.Entity() + .HasOne(e => e.Machine) + .WithMany(m => m.Events) + .HasForeignKey(e => e.MachineID) + .OnDelete(DeleteBehavior.Restrict); + + // Report and User (CreatedByUser) + modelBuilder.Entity() + .HasOne(r => r.CreatedByUser) + .WithMany() + .HasForeignKey(r => r.CreatedByUserID) + .OnDelete(DeleteBehavior.Restrict); + + // Event and User + modelBuilder.Entity() + .HasOne(e => e.User) + .WithMany() + .HasForeignKey(e => e.UserID) + .OnDelete(DeleteBehavior.Restrict); + + // Enum Conversions + modelBuilder.Entity() + .Property(m => m.Status) + .HasConversion(); + + modelBuilder.Entity() + .Property(a => a.Condition) + .HasConversion(); + + // Default Values for CreatedAt + modelBuilder.Entity() + .Property(a => a.CreatedAt) + .HasDefaultValueSql("GETDATE()"); + + modelBuilder.Entity() + .Property(e => e.CreatedAt) + .HasDefaultValueSql("GETDATE()"); + + + // Indexes + modelBuilder.Entity() + .HasIndex(m => m.FactoryID) + .HasDatabaseName("IX_Machine_FactoryID"); + + modelBuilder.Entity() + .HasIndex(l => l.FactoryID) + .HasDatabaseName("IX_Line_FactoryID"); + + modelBuilder.Entity() + .HasIndex(r => r.FactoryID) + .HasDatabaseName("IX_Report_FactoryID"); + + modelBuilder.Entity() + .HasIndex(a => a.FactoryID) + .HasDatabaseName("IX_Alert_FactoryID"); + + modelBuilder.Entity() + .HasIndex(e => e.FactoryID) + .HasDatabaseName("IX_Event_FactoryID"); + } } } diff --git a/ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.Designer.cs b/ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.Designer.cs new file mode 100644 index 0000000..5a62f6e --- /dev/null +++ b/ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.Designer.cs @@ -0,0 +1,482 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using ProtoSCADA.Data.Context; + +#nullable disable + +namespace ProtoSCADA.Data.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241230090714_AddLineAndReportTablesToProject")] + partial class AddLineAndReportTablesToProject + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Proxies:ChangeTracking", false) + .HasAnnotation("Proxies:CheckEquality", false) + .HasAnnotation("Proxies:LazyLoading", true) + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Alert", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Condition") + .HasColumnType("tinyint"); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("MachineID") + .HasColumnType("int"); + + b.Property("ThersholdValue") + .HasColumnType("real"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("MachineID"); + + b.HasIndex("UserID"); + + b.ToTable("Alerts"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Event", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MachineID") + .HasColumnType("int"); + + b.Property("TimeStamp") + .HasColumnType("datetime2"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("MachineID"); + + b.HasIndex("UserID"); + + b.ToTable("Events"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Factory", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("Location") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.ToTable("Factorys"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedDate") + .HasColumnType("datetime2"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("IsActive") + .HasColumnType("bit"); + + b.Property("LastMaintenanceDate") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ReportID") + .HasColumnType("int"); + + b.Property("SupervisorID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID"); + + b.HasIndex("ReportID"); + + b.HasIndex("SupervisorID"); + + b.ToTable("Lines"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LastMaintance") + .HasColumnType("datetime2"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ReportID") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("tinyint"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID"); + + b.HasIndex("LineID"); + + b.HasIndex("ReportID"); + + b.ToTable("Machines"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Metric", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("MachineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ReportID") + .HasColumnType("int"); + + b.Property("TimeStamp") + .HasColumnType("datetime2"); + + b.Property("Value") + .HasColumnType("real"); + + b.HasKey("ID"); + + b.HasIndex("MachineID"); + + b.HasIndex("ReportID"); + + b.ToTable("Metrics"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedByUserID") + .HasColumnType("int"); + + b.Property("CreatedDate") + .HasColumnType("datetime2"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IsArchived") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Tags") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("CreatedByUserID"); + + b.HasIndex("FactoryID"); + + b.ToTable("Reports"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Role") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Alert", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") + .WithMany("Alerts") + .HasForeignKey("MachineID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.User", null) + .WithMany("Alerts") + .HasForeignKey("UserID"); + + b.Navigation("Machine"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Event", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") + .WithMany("Events") + .HasForeignKey("MachineID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.User", "User") + .WithMany() + .HasForeignKey("UserID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Machine"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany() + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Report", null) + .WithMany("Lines") + .HasForeignKey("ReportID"); + + b.HasOne("ProtoSCADA.Entities.Entities.User", "Supervisor") + .WithMany() + .HasForeignKey("SupervisorID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Supervisor"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Machines") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", null) + .WithMany("Machines") + .HasForeignKey("LineID"); + + b.HasOne("ProtoSCADA.Entities.Entities.Report", null) + .WithMany("Machines") + .HasForeignKey("ReportID"); + + b.Navigation("Factory"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Metric", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") + .WithMany("Metrics") + .HasForeignKey("MachineID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Report", null) + .WithMany("Metrics") + .HasForeignKey("ReportID"); + + b.Navigation("Machine"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.User", "CreatedByUser") + .WithMany() + .HasForeignKey("CreatedByUserID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany() + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("CreatedByUser"); + + b.Navigation("Factory"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", null) + .WithMany("Users") + .HasForeignKey("FactoryID"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Factory", b => + { + b.Navigation("Machines"); + + b.Navigation("Users"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.Navigation("Machines"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => + { + b.Navigation("Alerts"); + + b.Navigation("Events"); + + b.Navigation("Metrics"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.Navigation("Lines"); + + b.Navigation("Machines"); + + b.Navigation("Metrics"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => + { + b.Navigation("Alerts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.cs b/ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.cs new file mode 100644 index 0000000..36682fd --- /dev/null +++ b/ProtoSCADA.DataService/Migrations/20241230090714_AddLineAndReportTablesToProject.cs @@ -0,0 +1,210 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ProtoSCADA.Data.Migrations +{ + /// + public partial class AddLineAndReportTablesToProject : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ReportID", + table: "Metrics", + type: "int", + nullable: true); + + migrationBuilder.AddColumn( + name: "LineID", + table: "Machines", + type: "int", + nullable: true); + + migrationBuilder.AddColumn( + name: "ReportID", + table: "Machines", + type: "int", + nullable: true); + + migrationBuilder.CreateTable( + name: "Reports", + columns: table => new + { + ID = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: false), + Type = table.Column(type: "nvarchar(max)", nullable: false), + CreatedDate = table.Column(type: "datetime2", nullable: false), + CreatedByUserID = table.Column(type: "int", nullable: false), + IsArchived = table.Column(type: "bit", nullable: false), + FactoryID = table.Column(type: "int", nullable: false), + Tags = table.Column(type: "nvarchar(200)", maxLength: 200, nullable: false), + FilePath = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Reports", x => x.ID); + table.ForeignKey( + name: "FK_Reports_Factorys_FactoryID", + column: x => x.FactoryID, + principalTable: "Factorys", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Reports_Users_CreatedByUserID", + column: x => x.CreatedByUserID, + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Lines", + columns: table => new + { + ID = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Description = table.Column(type: "nvarchar(max)", nullable: false), + FactoryID = table.Column(type: "int", nullable: false), + CreatedDate = table.Column(type: "datetime2", nullable: false), + LastMaintenanceDate = table.Column(type: "datetime2", nullable: true), + IsActive = table.Column(type: "bit", nullable: false), + SupervisorID = table.Column(type: "int", nullable: false), + ReportID = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Lines", x => x.ID); + table.ForeignKey( + name: "FK_Lines_Factorys_FactoryID", + column: x => x.FactoryID, + principalTable: "Factorys", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Lines_Reports_ReportID", + column: x => x.ReportID, + principalTable: "Reports", + principalColumn: "ID"); + table.ForeignKey( + name: "FK_Lines_Users_SupervisorID", + column: x => x.SupervisorID, + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Metrics_ReportID", + table: "Metrics", + column: "ReportID"); + + migrationBuilder.CreateIndex( + name: "IX_Machines_LineID", + table: "Machines", + column: "LineID"); + + migrationBuilder.CreateIndex( + name: "IX_Machines_ReportID", + table: "Machines", + column: "ReportID"); + + migrationBuilder.CreateIndex( + name: "IX_Lines_FactoryID", + table: "Lines", + column: "FactoryID"); + + migrationBuilder.CreateIndex( + name: "IX_Lines_ReportID", + table: "Lines", + column: "ReportID"); + + migrationBuilder.CreateIndex( + name: "IX_Lines_SupervisorID", + table: "Lines", + column: "SupervisorID"); + + migrationBuilder.CreateIndex( + name: "IX_Reports_CreatedByUserID", + table: "Reports", + column: "CreatedByUserID"); + + migrationBuilder.CreateIndex( + name: "IX_Reports_FactoryID", + table: "Reports", + column: "FactoryID"); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Lines_LineID", + table: "Machines", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID"); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Reports_ReportID", + table: "Machines", + column: "ReportID", + principalTable: "Reports", + principalColumn: "ID"); + + migrationBuilder.AddForeignKey( + name: "FK_Metrics_Reports_ReportID", + table: "Metrics", + column: "ReportID", + principalTable: "Reports", + principalColumn: "ID"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Machines_Lines_LineID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Machines_Reports_ReportID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Metrics_Reports_ReportID", + table: "Metrics"); + + migrationBuilder.DropTable( + name: "Lines"); + + migrationBuilder.DropTable( + name: "Reports"); + + migrationBuilder.DropIndex( + name: "IX_Metrics_ReportID", + table: "Metrics"); + + migrationBuilder.DropIndex( + name: "IX_Machines_LineID", + table: "Machines"); + + migrationBuilder.DropIndex( + name: "IX_Machines_ReportID", + table: "Machines"); + + migrationBuilder.DropColumn( + name: "ReportID", + table: "Metrics"); + + migrationBuilder.DropColumn( + name: "LineID", + table: "Machines"); + + migrationBuilder.DropColumn( + name: "ReportID", + table: "Machines"); + } + } +} diff --git a/ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.Designer.cs b/ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.Designer.cs new file mode 100644 index 0000000..1935cfc --- /dev/null +++ b/ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.Designer.cs @@ -0,0 +1,591 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using ProtoSCADA.Data.Context; + +#nullable disable + +namespace ProtoSCADA.Data.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241230122026_UpdateRelationshipsAndSchema")] + partial class UpdateRelationshipsAndSchema + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Proxies:ChangeTracking", false) + .HasAnnotation("Proxies:CheckEquality", false) + .HasAnnotation("Proxies:LazyLoading", true) + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Alert", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Condition") + .HasColumnType("tinyint"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETDATE()"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("MachineID") + .HasColumnType("int"); + + b.Property("ThresholdValue") + .HasColumnType("real"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Alert_FactoryID"); + + b.HasIndex("LineID"); + + b.HasIndex("MachineID"); + + b.HasIndex("UserID"); + + b.ToTable("Alerts"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Event", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETDATE()"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("MachineID") + .HasColumnType("int"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Event_FactoryID"); + + b.HasIndex("LineID"); + + b.HasIndex("MachineID"); + + b.HasIndex("UserID"); + + b.ToTable("Events"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Factory", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("Location") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.ToTable("Factories"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("CreatedDate") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETDATE()"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("IsActive") + .HasColumnType("bit"); + + b.Property("LastMaintenanceDate") + .HasColumnType("datetime2"); + + b.Property("LineNumber") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SupervisorID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Line_FactoryID"); + + b.HasIndex("SupervisorID"); + + b.ToTable("Lines"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LastMaintenance") + .HasColumnType("datetime2"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("tinyint"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Machine_FactoryID"); + + b.HasIndex("LineID"); + + b.ToTable("Machines"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Metric", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("MachineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Value") + .HasColumnType("real"); + + b.HasKey("ID"); + + b.HasIndex("MachineID"); + + b.ToTable("Metrics"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("CreatedByUserID") + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IsArchived") + .HasColumnType("bit"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Tags") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("CreatedByUserID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Report_FactoryID"); + + b.HasIndex("LineID"); + + b.ToTable("Reports"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Role", b => + { + b.Property("RoleID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RoleID")); + + b.Property("RoleName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("RoleID"); + + b.ToTable("Roles"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RoleID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID"); + + b.HasIndex("LineID"); + + b.HasIndex("RoleID"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Alert", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Alerts") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Alerts") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") + .WithMany("Alerts") + .HasForeignKey("MachineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.User", null) + .WithMany("Alerts") + .HasForeignKey("UserID"); + + b.Navigation("Factory"); + + b.Navigation("Line"); + + b.Navigation("Machine"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Event", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Events") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Events") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") + .WithMany("Events") + .HasForeignKey("MachineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.User", "User") + .WithMany() + .HasForeignKey("UserID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Line"); + + b.Navigation("Machine"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Lines") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.User", "Supervisor") + .WithMany() + .HasForeignKey("SupervisorID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Supervisor"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Machines") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Machines") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Line"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Metric", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") + .WithMany("Metrics") + .HasForeignKey("MachineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Machine"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.User", "CreatedByUser") + .WithMany() + .HasForeignKey("CreatedByUserID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Reports") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Reports") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("CreatedByUser"); + + b.Navigation("Factory"); + + b.Navigation("Line"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Users") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany() + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Role", "Role") + .WithMany() + .HasForeignKey("RoleID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Line"); + + b.Navigation("Role"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Factory", b => + { + b.Navigation("Alerts"); + + b.Navigation("Events"); + + b.Navigation("Lines"); + + b.Navigation("Machines"); + + b.Navigation("Reports"); + + b.Navigation("Users"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.Navigation("Alerts"); + + b.Navigation("Events"); + + b.Navigation("Machines"); + + b.Navigation("Reports"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => + { + b.Navigation("Alerts"); + + b.Navigation("Events"); + + b.Navigation("Metrics"); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => + { + b.Navigation("Alerts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.cs b/ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.cs new file mode 100644 index 0000000..32bdf0b --- /dev/null +++ b/ProtoSCADA.DataService/Migrations/20241230122026_UpdateRelationshipsAndSchema.cs @@ -0,0 +1,847 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ProtoSCADA.Data.Migrations +{ + /// + public partial class UpdateRelationshipsAndSchema : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Alerts_Machines_MachineID", + table: "Alerts"); + + migrationBuilder.DropForeignKey( + name: "FK_Events_Machines_MachineID", + table: "Events"); + + migrationBuilder.DropForeignKey( + name: "FK_Events_Users_UserID", + table: "Events"); + + migrationBuilder.DropForeignKey( + name: "FK_Lines_Factorys_FactoryID", + table: "Lines"); + + migrationBuilder.DropForeignKey( + name: "FK_Lines_Reports_ReportID", + table: "Lines"); + + migrationBuilder.DropForeignKey( + name: "FK_Lines_Users_SupervisorID", + table: "Lines"); + + migrationBuilder.DropForeignKey( + name: "FK_Machines_Factorys_FactoryID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Machines_Lines_LineID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Machines_Reports_ReportID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Metrics_Machines_MachineID", + table: "Metrics"); + + migrationBuilder.DropForeignKey( + name: "FK_Metrics_Reports_ReportID", + table: "Metrics"); + + migrationBuilder.DropForeignKey( + name: "FK_Reports_Factorys_FactoryID", + table: "Reports"); + + migrationBuilder.DropForeignKey( + name: "FK_Reports_Users_CreatedByUserID", + table: "Reports"); + + migrationBuilder.DropForeignKey( + name: "FK_Users_Factorys_FactoryID", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Metrics_ReportID", + table: "Metrics"); + + migrationBuilder.DropIndex( + name: "IX_Machines_ReportID", + table: "Machines"); + + migrationBuilder.DropIndex( + name: "IX_Lines_ReportID", + table: "Lines"); + + migrationBuilder.DropPrimaryKey( + name: "PK_Factorys", + table: "Factorys"); + + migrationBuilder.DropColumn( + name: "ReportID", + table: "Metrics"); + + migrationBuilder.DropColumn( + name: "ReportID", + table: "Machines"); + + migrationBuilder.DropColumn( + name: "ReportID", + table: "Lines"); + + migrationBuilder.DropColumn( + name: "TimeStamp", + table: "Events"); + + migrationBuilder.RenameTable( + name: "Factorys", + newName: "Factories"); + + migrationBuilder.RenameColumn( + name: "CreatedDate", + table: "Reports", + newName: "CreatedAt"); + + migrationBuilder.RenameIndex( + name: "IX_Reports_FactoryID", + table: "Reports", + newName: "IX_Report_FactoryID"); + + migrationBuilder.RenameColumn( + name: "TimeStamp", + table: "Metrics", + newName: "CreatedAt"); + + migrationBuilder.RenameColumn( + name: "LastMaintance", + table: "Machines", + newName: "LastMaintenance"); + + migrationBuilder.RenameIndex( + name: "IX_Machines_FactoryID", + table: "Machines", + newName: "IX_Machine_FactoryID"); + + migrationBuilder.RenameIndex( + name: "IX_Lines_FactoryID", + table: "Lines", + newName: "IX_Line_FactoryID"); + + migrationBuilder.RenameColumn( + name: "ThersholdValue", + table: "Alerts", + newName: "ThresholdValue"); + + migrationBuilder.AlterColumn( + name: "FactoryID", + table: "Users", + type: "int", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "int", + oldNullable: true); + + migrationBuilder.AddColumn( + name: "LineID", + table: "Users", + type: "int", + nullable: false, + defaultValue: 0); + + // Remove the duplicate RoleID column addition + // migrationBuilder.AddColumn( + // name: "RoleID", + // table: "Users", + // type: "int", + // nullable: false, + // defaultValue: 0); + + migrationBuilder.AlterColumn( + name: "Tags", + table: "Reports", + type: "nvarchar(max)", + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(200)", + oldMaxLength: 200); + + migrationBuilder.AddColumn( + name: "LineID", + table: "Reports", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.AlterColumn( + name: "LineID", + table: "Machines", + type: "int", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "int", + oldNullable: true); + + migrationBuilder.AddColumn( + name: "CreatedAt", + table: "Machines", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified)); + + migrationBuilder.AlterColumn( + name: "CreatedDate", + table: "Lines", + type: "datetime2", + nullable: false, + defaultValueSql: "GETDATE()", + oldClrType: typeof(DateTime), + oldType: "datetime2"); + + migrationBuilder.AddColumn( + name: "CreatedAt", + table: "Lines", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified)); + + migrationBuilder.AddColumn( + name: "LineNumber", + table: "Lines", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "CreatedAt", + table: "Events", + type: "datetime2", + nullable: false, + defaultValueSql: "GETDATE()"); + + migrationBuilder.AddColumn( + name: "FactoryID", + table: "Events", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "LineID", + table: "Events", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.AlterColumn( + name: "CreatedAt", + table: "Alerts", + type: "datetime2", + nullable: false, + defaultValueSql: "GETDATE()", + oldClrType: typeof(DateTime), + oldType: "datetime2"); + + migrationBuilder.AddColumn( + name: "FactoryID", + table: "Alerts", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "LineID", + table: "Alerts", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddPrimaryKey( + name: "PK_Factories", + table: "Factories", + column: "ID"); + + migrationBuilder.CreateIndex( + name: "IX_Users_LineID", + table: "Users", + column: "LineID"); + + migrationBuilder.CreateIndex( + name: "IX_Users_RoleID", + table: "Users", + column: "RoleID"); + + migrationBuilder.CreateIndex( + name: "IX_Reports_LineID", + table: "Reports", + column: "LineID"); + + migrationBuilder.CreateIndex( + name: "IX_Event_FactoryID", + table: "Events", + column: "FactoryID"); + + migrationBuilder.CreateIndex( + name: "IX_Events_LineID", + table: "Events", + column: "LineID"); + + migrationBuilder.CreateIndex( + name: "IX_Alert_FactoryID", + table: "Alerts", + column: "FactoryID"); + + migrationBuilder.CreateIndex( + name: "IX_Alerts_LineID", + table: "Alerts", + column: "LineID"); + + migrationBuilder.AddForeignKey( + name: "FK_Alerts_Factories_FactoryID", + table: "Alerts", + column: "FactoryID", + principalTable: "Factories", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Alerts_Lines_LineID", + table: "Alerts", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Alerts_Machines_MachineID", + table: "Alerts", + column: "MachineID", + principalTable: "Machines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Events_Factories_FactoryID", + table: "Events", + column: "FactoryID", + principalTable: "Factories", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Events_Lines_LineID", + table: "Events", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Events_Machines_MachineID", + table: "Events", + column: "MachineID", + principalTable: "Machines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Events_Users_UserID", + table: "Events", + column: "UserID", + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Lines_Factories_FactoryID", + table: "Lines", + column: "FactoryID", + principalTable: "Factories", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Lines_Users_SupervisorID", + table: "Lines", + column: "SupervisorID", + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Factories_FactoryID", + table: "Machines", + column: "FactoryID", + principalTable: "Factories", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Lines_LineID", + table: "Machines", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Metrics_Machines_MachineID", + table: "Metrics", + column: "MachineID", + principalTable: "Machines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Reports_Factories_FactoryID", + table: "Reports", + column: "FactoryID", + principalTable: "Factories", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Reports_Lines_LineID", + table: "Reports", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Reports_Users_CreatedByUserID", + table: "Reports", + column: "CreatedByUserID", + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Users_Factories_FactoryID", + table: "Users", + column: "FactoryID", + principalTable: "Factories", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Users_Lines_LineID", + table: "Users", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Users_Roles_RoleID", + table: "Users", + column: "RoleID", + principalTable: "Roles", + principalColumn: "RoleID", + onDelete: ReferentialAction.Restrict); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Alerts_Factories_FactoryID", + table: "Alerts"); + + migrationBuilder.DropForeignKey( + name: "FK_Alerts_Lines_LineID", + table: "Alerts"); + + migrationBuilder.DropForeignKey( + name: "FK_Alerts_Machines_MachineID", + table: "Alerts"); + + migrationBuilder.DropForeignKey( + name: "FK_Events_Factories_FactoryID", + table: "Events"); + + migrationBuilder.DropForeignKey( + name: "FK_Events_Lines_LineID", + table: "Events"); + + migrationBuilder.DropForeignKey( + name: "FK_Events_Machines_MachineID", + table: "Events"); + + migrationBuilder.DropForeignKey( + name: "FK_Events_Users_UserID", + table: "Events"); + + migrationBuilder.DropForeignKey( + name: "FK_Lines_Factories_FactoryID", + table: "Lines"); + + migrationBuilder.DropForeignKey( + name: "FK_Lines_Users_SupervisorID", + table: "Lines"); + + migrationBuilder.DropForeignKey( + name: "FK_Machines_Factories_FactoryID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Machines_Lines_LineID", + table: "Machines"); + + migrationBuilder.DropForeignKey( + name: "FK_Metrics_Machines_MachineID", + table: "Metrics"); + + migrationBuilder.DropForeignKey( + name: "FK_Reports_Factories_FactoryID", + table: "Reports"); + + migrationBuilder.DropForeignKey( + name: "FK_Reports_Lines_LineID", + table: "Reports"); + + migrationBuilder.DropForeignKey( + name: "FK_Reports_Users_CreatedByUserID", + table: "Reports"); + + migrationBuilder.DropForeignKey( + name: "FK_Users_Factories_FactoryID", + table: "Users"); + + migrationBuilder.DropForeignKey( + name: "FK_Users_Lines_LineID", + table: "Users"); + + migrationBuilder.DropForeignKey( + name: "FK_Users_Roles_RoleID", + table: "Users"); + + migrationBuilder.DropTable( + name: "Roles"); + + migrationBuilder.DropIndex( + name: "IX_Users_LineID", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Users_RoleID", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Reports_LineID", + table: "Reports"); + + migrationBuilder.DropIndex( + name: "IX_Event_FactoryID", + table: "Events"); + + migrationBuilder.DropIndex( + name: "IX_Events_LineID", + table: "Events"); + + migrationBuilder.DropIndex( + name: "IX_Alert_FactoryID", + table: "Alerts"); + + migrationBuilder.DropIndex( + name: "IX_Alerts_LineID", + table: "Alerts"); + + migrationBuilder.DropPrimaryKey( + name: "PK_Factories", + table: "Factories"); + + migrationBuilder.DropColumn( + name: "LineID", + table: "Users"); + + migrationBuilder.DropColumn( + name: "RoleID", + table: "Users"); + + migrationBuilder.DropColumn( + name: "LineID", + table: "Reports"); + + migrationBuilder.DropColumn( + name: "CreatedAt", + table: "Machines"); + + migrationBuilder.DropColumn( + name: "CreatedAt", + table: "Lines"); + + migrationBuilder.DropColumn( + name: "LineNumber", + table: "Lines"); + + migrationBuilder.DropColumn( + name: "CreatedAt", + table: "Events"); + + migrationBuilder.DropColumn( + name: "FactoryID", + table: "Events"); + + migrationBuilder.DropColumn( + name: "LineID", + table: "Events"); + + migrationBuilder.DropColumn( + name: "FactoryID", + table: "Alerts"); + + migrationBuilder.DropColumn( + name: "LineID", + table: "Alerts"); + + migrationBuilder.RenameTable( + name: "Factories", + newName: "Factorys"); + + migrationBuilder.RenameColumn( + name: "CreatedAt", + table: "Reports", + newName: "CreatedDate"); + + migrationBuilder.RenameIndex( + name: "IX_Report_FactoryID", + table: "Reports", + newName: "IX_Reports_FactoryID"); + + migrationBuilder.RenameColumn( + name: "CreatedAt", + table: "Metrics", + newName: "TimeStamp"); + + migrationBuilder.RenameColumn( + name: "LastMaintenance", + table: "Machines", + newName: "LastMaintance"); + + migrationBuilder.RenameIndex( + name: "IX_Machine_FactoryID", + table: "Machines", + newName: "IX_Machines_FactoryID"); + + migrationBuilder.RenameIndex( + name: "IX_Line_FactoryID", + table: "Lines", + newName: "IX_Lines_FactoryID"); + + migrationBuilder.RenameColumn( + name: "ThresholdValue", + table: "Alerts", + newName: "ThersholdValue"); + + migrationBuilder.AlterColumn( + name: "FactoryID", + table: "Users", + type: "int", + nullable: true, + oldClrType: typeof(int), + oldType: "int"); + + migrationBuilder.AddColumn( + name: "Role", + table: "Users", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + + migrationBuilder.AlterColumn( + name: "Tags", + table: "Reports", + type: "nvarchar(200)", + maxLength: 200, + nullable: false, + oldClrType: typeof(string), + oldType: "nvarchar(max)"); + + migrationBuilder.AddColumn( + name: "ReportID", + table: "Metrics", + type: "int", + nullable: true); + + migrationBuilder.AlterColumn( + name: "LineID", + table: "Machines", + type: "int", + nullable: true, + oldClrType: typeof(int), + oldType: "int"); + + migrationBuilder.AddColumn( + name: "ReportID", + table: "Machines", + type: "int", + nullable: true); + + migrationBuilder.AlterColumn( + name: "CreatedDate", + table: "Lines", + type: "datetime2", + nullable: false, + oldClrType: typeof(DateTime), + oldType: "datetime2", + oldDefaultValueSql: "GETDATE()"); + + migrationBuilder.AddColumn( + name: "ReportID", + table: "Lines", + type: "int", + nullable: true); + + migrationBuilder.AddColumn( + name: "TimeStamp", + table: "Events", + type: "datetime2", + nullable: false, + defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified)); + + migrationBuilder.AlterColumn( + name: "CreatedAt", + table: "Alerts", + type: "datetime2", + nullable: false, + oldClrType: typeof(DateTime), + oldType: "datetime2", + oldDefaultValueSql: "GETDATE()"); + + migrationBuilder.AddPrimaryKey( + name: "PK_Factorys", + table: "Factorys", + column: "ID"); + + migrationBuilder.CreateIndex( + name: "IX_Metrics_ReportID", + table: "Metrics", + column: "ReportID"); + + migrationBuilder.CreateIndex( + name: "IX_Machines_ReportID", + table: "Machines", + column: "ReportID"); + + migrationBuilder.CreateIndex( + name: "IX_Lines_ReportID", + table: "Lines", + column: "ReportID"); + + migrationBuilder.AddForeignKey( + name: "FK_Alerts_Machines_MachineID", + table: "Alerts", + column: "MachineID", + principalTable: "Machines", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Events_Machines_MachineID", + table: "Events", + column: "MachineID", + principalTable: "Machines", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Events_Users_UserID", + table: "Events", + column: "UserID", + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Lines_Factorys_FactoryID", + table: "Lines", + column: "FactoryID", + principalTable: "Factorys", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Lines_Reports_ReportID", + table: "Lines", + column: "ReportID", + principalTable: "Reports", + principalColumn: "ID"); + + migrationBuilder.AddForeignKey( + name: "FK_Lines_Users_SupervisorID", + table: "Lines", + column: "SupervisorID", + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Factorys_FactoryID", + table: "Machines", + column: "FactoryID", + principalTable: "Factorys", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Lines_LineID", + table: "Machines", + column: "LineID", + principalTable: "Lines", + principalColumn: "ID"); + + migrationBuilder.AddForeignKey( + name: "FK_Machines_Reports_ReportID", + table: "Machines", + column: "ReportID", + principalTable: "Reports", + principalColumn: "ID"); + + migrationBuilder.AddForeignKey( + name: "FK_Metrics_Machines_MachineID", + table: "Metrics", + column: "MachineID", + principalTable: "Machines", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Metrics_Reports_ReportID", + table: "Metrics", + column: "ReportID", + principalTable: "Reports", + principalColumn: "ID"); + + migrationBuilder.AddForeignKey( + name: "FK_Reports_Factorys_FactoryID", + table: "Reports", + column: "FactoryID", + principalTable: "Factorys", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Reports_Users_CreatedByUserID", + table: "Reports", + column: "CreatedByUserID", + principalTable: "Users", + principalColumn: "ID", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_Users_Factorys_FactoryID", + table: "Users", + column: "FactoryID", + principalTable: "Factorys", + principalColumn: "ID"); + } + } +} diff --git a/ProtoSCADA.DataService/Migrations/ApplicationDbContextModelSnapshot.cs b/ProtoSCADA.DataService/Migrations/ApplicationDbContextModelSnapshot.cs index bcfc915..dcd37e2 100644 --- a/ProtoSCADA.DataService/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/ProtoSCADA.DataService/Migrations/ApplicationDbContextModelSnapshot.cs @@ -18,6 +18,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Proxies:ChangeTracking", false) + .HasAnnotation("Proxies:CheckEquality", false) + .HasAnnotation("Proxies:LazyLoading", true) .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); @@ -34,12 +37,20 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("tinyint"); b.Property("CreatedAt") - .HasColumnType("datetime2"); + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETDATE()"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LineID") + .HasColumnType("int"); b.Property("MachineID") .HasColumnType("int"); - b.Property("ThersholdValue") + b.Property("ThresholdValue") .HasColumnType("real"); b.Property("UserID") @@ -47,6 +58,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("ID"); + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Alert_FactoryID"); + + b.HasIndex("LineID"); + b.HasIndex("MachineID"); b.HasIndex("UserID"); @@ -62,15 +78,23 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETDATE()"); + b.Property("Description") .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("MachineID") + b.Property("FactoryID") .HasColumnType("int"); - b.Property("TimeStamp") - .HasColumnType("datetime2"); + b.Property("LineID") + .HasColumnType("int"); + + b.Property("MachineID") + .HasColumnType("int"); b.Property("Type") .IsRequired() @@ -81,6 +105,11 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("ID"); + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Event_FactoryID"); + + b.HasIndex("LineID"); + b.HasIndex("MachineID"); b.HasIndex("UserID"); @@ -109,7 +138,56 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("ID"); - b.ToTable("Factorys", (string)null); + b.ToTable("Factories", (string)null); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("CreatedDate") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("GETDATE()"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("IsActive") + .HasColumnType("bit"); + + b.Property("LastMaintenanceDate") + .HasColumnType("datetime2"); + + b.Property("LineNumber") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SupervisorID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Line_FactoryID"); + + b.HasIndex("SupervisorID"); + + b.ToTable("Lines", (string)null); }); modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => @@ -120,12 +198,18 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + b.Property("CreatedAt") + .HasColumnType("datetime2"); + b.Property("FactoryID") .HasColumnType("int"); - b.Property("LastMaintance") + b.Property("LastMaintenance") .HasColumnType("datetime2"); + b.Property("LineID") + .HasColumnType("int"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -139,7 +223,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("ID"); - b.HasIndex("FactoryID"); + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Machine_FactoryID"); + + b.HasIndex("LineID"); b.ToTable("Machines", (string)null); }); @@ -152,6 +239,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + b.Property("CreatedAt") + .HasColumnType("datetime2"); + b.Property("MachineID") .HasColumnType("int"); @@ -159,9 +249,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("TimeStamp") - .HasColumnType("datetime2"); - b.Property("Value") .HasColumnType("real"); @@ -172,6 +259,78 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.ToTable("Metrics", (string)null); }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("CreatedAt") + .HasColumnType("datetime2"); + + b.Property("CreatedByUserID") + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IsArchived") + .HasColumnType("bit"); + + b.Property("LineID") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Tags") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Type") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("CreatedByUserID"); + + b.HasIndex("FactoryID") + .HasDatabaseName("IX_Report_FactoryID"); + + b.HasIndex("LineID"); + + b.ToTable("Reports", (string)null); + }); + + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Role", b => + { + b.Property("RoleID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("RoleID")); + + b.Property("RoleName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("RoleID"); + + b.ToTable("Roles", (string)null); + }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => { b.Property("ID") @@ -187,7 +346,10 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("FactoryID") + b.Property("FactoryID") + .HasColumnType("int"); + + b.Property("LineID") .HasColumnType("int"); b.Property("Name") @@ -198,60 +360,122 @@ protected override void BuildModel(ModelBuilder modelBuilder) .IsRequired() .HasColumnType("nvarchar(max)"); - b.Property("Role") - .IsRequired() - .HasColumnType("nvarchar(max)"); + b.Property("RoleID") + .HasColumnType("int"); b.HasKey("ID"); b.HasIndex("FactoryID"); + b.HasIndex("LineID"); + + b.HasIndex("RoleID"); + b.ToTable("Users", (string)null); }); modelBuilder.Entity("ProtoSCADA.Entities.Entities.Alert", b => { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Alerts") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Alerts") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") .WithMany("Alerts") .HasForeignKey("MachineID") - .OnDelete(DeleteBehavior.Cascade) + .OnDelete(DeleteBehavior.Restrict) .IsRequired(); b.HasOne("ProtoSCADA.Entities.Entities.User", null) .WithMany("Alerts") .HasForeignKey("UserID"); + b.Navigation("Factory"); + + b.Navigation("Line"); + b.Navigation("Machine"); }); modelBuilder.Entity("ProtoSCADA.Entities.Entities.Event", b => { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Events") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Events") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") .WithMany("Events") .HasForeignKey("MachineID") - .OnDelete(DeleteBehavior.Cascade) + .OnDelete(DeleteBehavior.Restrict) .IsRequired(); b.HasOne("ProtoSCADA.Entities.Entities.User", "User") .WithMany() .HasForeignKey("UserID") - .OnDelete(DeleteBehavior.Cascade) + .OnDelete(DeleteBehavior.Restrict) .IsRequired(); + b.Navigation("Factory"); + + b.Navigation("Line"); + b.Navigation("Machine"); b.Navigation("User"); }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Lines") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.User", "Supervisor") + .WithMany() + .HasForeignKey("SupervisorID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Supervisor"); + }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => { b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") .WithMany("Machines") .HasForeignKey("FactoryID") - .OnDelete(DeleteBehavior.Cascade) + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Machines") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) .IsRequired(); b.Navigation("Factory"); + + b.Navigation("Line"); }); modelBuilder.Entity("ProtoSCADA.Entities.Entities.Metric", b => @@ -259,26 +483,92 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasOne("ProtoSCADA.Entities.Entities.Machine", "Machine") .WithMany("Metrics") .HasForeignKey("MachineID") - .OnDelete(DeleteBehavior.Cascade) + .OnDelete(DeleteBehavior.Restrict) .IsRequired(); b.Navigation("Machine"); }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Report", b => + { + b.HasOne("ProtoSCADA.Entities.Entities.User", "CreatedByUser") + .WithMany() + .HasForeignKey("CreatedByUserID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") + .WithMany("Reports") + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany("Reports") + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("CreatedByUser"); + + b.Navigation("Factory"); + + b.Navigation("Line"); + }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.User", b => { - b.HasOne("ProtoSCADA.Entities.Entities.Factory", null) + b.HasOne("ProtoSCADA.Entities.Entities.Factory", "Factory") .WithMany("Users") - .HasForeignKey("FactoryID"); + .HasForeignKey("FactoryID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Line", "Line") + .WithMany() + .HasForeignKey("LineID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ProtoSCADA.Entities.Entities.Role", "Role") + .WithMany() + .HasForeignKey("RoleID") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.Navigation("Factory"); + + b.Navigation("Line"); + + b.Navigation("Role"); }); modelBuilder.Entity("ProtoSCADA.Entities.Entities.Factory", b => { + b.Navigation("Alerts"); + + b.Navigation("Events"); + + b.Navigation("Lines"); + b.Navigation("Machines"); + b.Navigation("Reports"); + b.Navigation("Users"); }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Line", b => + { + b.Navigation("Alerts"); + + b.Navigation("Events"); + + b.Navigation("Machines"); + + b.Navigation("Reports"); + }); + modelBuilder.Entity("ProtoSCADA.Entities.Entities.Machine", b => { b.Navigation("Alerts"); diff --git a/ProtoSCADA.Entities/Entities/Alert.cs b/ProtoSCADA.Entities/Entities/Alert.cs index b6d5158..5b75fdd 100644 --- a/ProtoSCADA.Entities/Entities/Alert.cs +++ b/ProtoSCADA.Entities/Entities/Alert.cs @@ -1,31 +1,27 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using ProtoSCADA.Entities.Entities.Base; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ProtoSCADA.Entities.Entities { - public class Alert + public class Alert : BaseEntity { - [Key] - public int ID { get; set; } - - public float ThersholdValue { get; set; } - + public float ThresholdValue { get; set; } public AlertCondition Condition { get; set; } - public DateTime CreatedAt { get; set; } - [ForeignKey("Machine")] public int MachineID { get; set; } public virtual Machine Machine { get; set; } + [ForeignKey("Line")] + public int LineID { get; set; } + public virtual Line Line { get; set; } + + [ForeignKey("Factory")] + public int FactoryID { get; set; } + public virtual Factory Factory { get; set; } } - public enum AlertCondition : byte // so that we can optimize storage + public enum AlertCondition : byte { GreaterThan, LessThan, Equal } diff --git a/ProtoSCADA.Entities/Entities/Base/BaseEntity.cs b/ProtoSCADA.Entities/Entities/Base/BaseEntity.cs new file mode 100644 index 0000000..693e622 --- /dev/null +++ b/ProtoSCADA.Entities/Entities/Base/BaseEntity.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProtoSCADA.Entities.Entities.Base +{ + public class BaseEntity + { + [Key] + public int ID { get; set; } + + public DateTime CreatedAt { get; set; } = DateTime.Now; + } +} diff --git a/ProtoSCADA.Entities/Entities/Event.cs b/ProtoSCADA.Entities/Entities/Event.cs index 9129dd9..724d64c 100644 --- a/ProtoSCADA.Entities/Entities/Event.cs +++ b/ProtoSCADA.Entities/Entities/Event.cs @@ -1,32 +1,27 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using ProtoSCADA.Entities.Entities.Base; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ProtoSCADA.Entities.Entities { - public class Event + public class Event : BaseEntity { - [Key] - public int ID { get; set; } - public string Type { get; set; } = string.Empty; - public string Description { get; set; } = string.Empty; - public DateTime TimeStamp { get; set; } - [ForeignKey("User")] public int UserID { get; set; } public virtual User User { get; set; } [ForeignKey("Machine")] - public int MachineID { get; set; } + public int MachineID { get; set; } public virtual Machine Machine { get; set; } + [ForeignKey("Line")] + public int LineID { get; set; } + public virtual Line Line { get; set; } + [ForeignKey("Factory")] + public int FactoryID { get; set; } + public virtual Factory Factory { get; set; } } } diff --git a/ProtoSCADA.Entities/Entities/Factory.cs b/ProtoSCADA.Entities/Entities/Factory.cs index 223a319..4cc95de 100644 --- a/ProtoSCADA.Entities/Entities/Factory.cs +++ b/ProtoSCADA.Entities/Entities/Factory.cs @@ -1,26 +1,17 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ProtoSCADA.Entities.Entities.Base; namespace ProtoSCADA.Entities.Entities { - public class Factory + public class Factory : BaseEntity { - [Key] - public int ID { get; set; } - public string Name { get; set; } = string.Empty; - public string Location { get; set; } = string.Empty; - public DateTime CreatedAt { get; set; } - - public virtual ICollection Users { get; set; } = new HashSet (); - + public virtual ICollection Users { get; set; } = new HashSet(); public virtual ICollection Machines { get; set; } = new HashSet(); - + public virtual ICollection Lines { get; set; } = new HashSet(); + public virtual ICollection Reports { get; set; } = new HashSet(); + public virtual ICollection Alerts { get; set; } = new HashSet(); + public virtual ICollection Events { get; set; } = new HashSet(); } -} +} \ No newline at end of file diff --git a/ProtoSCADA.Entities/Entities/Line.cs b/ProtoSCADA.Entities/Entities/Line.cs new file mode 100644 index 0000000..6e78029 --- /dev/null +++ b/ProtoSCADA.Entities/Entities/Line.cs @@ -0,0 +1,28 @@ +using ProtoSCADA.Entities.Entities.Base; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ProtoSCADA.Entities.Entities +{ + public class Line : BaseEntity + { + public string Name { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public int LineNumber { get; set; } + + [ForeignKey("Factory")] + public int FactoryID { get; set; } + public virtual Factory Factory { get; set; } + public DateTime? LastMaintenanceDate { get; set; } + public bool IsActive { get; set; } = true; + + + public virtual ICollection Machines { get; set; } = new HashSet(); + public virtual ICollection Alerts { get; set; } = new HashSet(); + public virtual ICollection Reports { get; set; } = new HashSet(); + public virtual ICollection Events { get; set; } = new HashSet(); + + [ForeignKey("Supervisor")] + public int SupervisorID { get; set; } + public virtual User Supervisor { get; set; } + } +} \ No newline at end of file diff --git a/ProtoSCADA.Entities/Entities/Machine.cs b/ProtoSCADA.Entities/Entities/Machine.cs index 2296bbe..18f6654 100644 --- a/ProtoSCADA.Entities/Entities/Machine.cs +++ b/ProtoSCADA.Entities/Entities/Machine.cs @@ -1,40 +1,30 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using ProtoSCADA.Entities.Entities.Base; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ProtoSCADA.Entities.Entities { - public class Machine + public class Machine : BaseEntity { - [Key] - public int ID { get; set; } - public string Name { get; set; } = string.Empty; - public string Type { get; set; } = string.Empty; - - public MachineStatus Status { get; set; } - - public DateTime LastMaintance { get; set; } + public MachineStatus Status { get; set; } + public DateTime LastMaintenance { get; set; } [ForeignKey("Factory")] public int FactoryID { get; set; } public virtual Factory Factory { get; set; } + [ForeignKey("Line")] + public int LineID { get; set; } + public virtual Line Line { get; set; } public virtual ICollection Alerts { get; set; } = new HashSet(); - public virtual ICollection Events { get; set; } = new HashSet(); - public virtual ICollection Metrics { get; set; } = new HashSet(); - } - public enum MachineStatus : byte // so that we can optimize storage + + public enum MachineStatus : byte { Running, Idle, Stopped } -} +} \ No newline at end of file diff --git a/ProtoSCADA.Entities/Entities/Metric.cs b/ProtoSCADA.Entities/Entities/Metric.cs index 077892d..16233a0 100644 --- a/ProtoSCADA.Entities/Entities/Metric.cs +++ b/ProtoSCADA.Entities/Entities/Metric.cs @@ -1,27 +1,15 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; +using ProtoSCADA.Entities.Entities.Base; using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ProtoSCADA.Entities.Entities { - public class Metric + public class Metric : BaseEntity { - [Key] - public int ID { get; set; } - - public DateTime TimeStamp { get; set; } - public string Name { get; set; } = string.Empty; - - public float Value { get; set; } - + public float Value { get; set; } [ForeignKey("Machine")] - public int MachineID { get; set; } + public int MachineID { get; set; } public virtual Machine Machine { get; set; } } -} +} \ No newline at end of file diff --git a/ProtoSCADA.Entities/Entities/Report.cs b/ProtoSCADA.Entities/Entities/Report.cs new file mode 100644 index 0000000..ae7e037 --- /dev/null +++ b/ProtoSCADA.Entities/Entities/Report.cs @@ -0,0 +1,29 @@ +using ProtoSCADA.Entities.Entities.Base; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ProtoSCADA.Entities.Entities +{ + public class Report : BaseEntity + { + public string Name { get; set; } + public string Description { get; set; } + public string Type { get; set; } + + [ForeignKey("User")] + public int CreatedByUserID { get; set; } + public virtual User CreatedByUser { get; set; } + + public bool IsArchived { get; set; } = false; + + [ForeignKey("Factory")] + public int FactoryID { get; set; } + public virtual Factory Factory { get; set; } + + [ForeignKey("Line")] + public int LineID { get; set; } + public virtual Line Line { get; set; } + + public string Tags { get; set; } + public string FilePath { get; set; } + } +} diff --git a/ProtoSCADA.Entities/Entities/Role.cs b/ProtoSCADA.Entities/Entities/Role.cs new file mode 100644 index 0000000..0341370 --- /dev/null +++ b/ProtoSCADA.Entities/Entities/Role.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace ProtoSCADA.Entities.Entities +{ + public class Role + { + [Key] + public int RoleID { get; set; } + + public string RoleName { get; set; } = string.Empty; + } +} diff --git a/ProtoSCADA.Entities/Entities/User.cs b/ProtoSCADA.Entities/Entities/User.cs index 11e73a5..0e91ef2 100644 --- a/ProtoSCADA.Entities/Entities/User.cs +++ b/ProtoSCADA.Entities/Entities/User.cs @@ -1,28 +1,26 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ProtoSCADA.Entities.Entities.Base; +using System.ComponentModel.DataAnnotations.Schema; namespace ProtoSCADA.Entities.Entities { - public class User + public class User : BaseEntity { - [Key] - public int ID { get; set; } - public string Name { get; set; } = string.Empty; - public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; - public string Password { get; set; } = string.Empty; + [ForeignKey("Factory")] + public int FactoryID { get; set; } + public virtual Factory Factory { get; set; } - public string Role { get; set; } = string.Empty; + [ForeignKey("Line")] + public int LineID { get; set; } + public virtual Line Line { get; set; } - public DateTime CreatedAt { get; set; } + [ForeignKey("Role")] + public int RoleID { get; set; } + public virtual Role Role { get; set; } public virtual ICollection Alerts { get; set; } = new HashSet(); - } -} +} \ No newline at end of file