using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Data.SeaHavenIndustries.Migrations
{
///
public partial class SH187_WorkOrderServiceLink : Migration
{
///
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn(
name: "ServiceId",
table: "workOrders",
type: "int",
nullable: true);
migrationBuilder.AddColumn(
name: "ServiceNameSnapshot",
table: "workOrders",
type: "nvarchar(200)",
maxLength: 200,
nullable: true);
migrationBuilder.Sql(
"""
UPDATE w
SET
w.[ServiceNameSnapshot] = LEFT(COALESCE(NULLIF(LTRIM(RTRIM(w.[Trade])), N''), NULLIF(LTRIM(RTRIM(w.[Problem])), N'')), 200),
w.[ServiceId] = s.[Id]
FROM [workOrders] w
LEFT JOIN [Services] s
ON (s.[IsDeleted] <> 1 OR s.[IsDeleted] IS NULL)
AND s.[NormalizedName] = LOWER(LTRIM(RTRIM(COALESCE(NULLIF(LTRIM(RTRIM(w.[Trade])), N''), NULLIF(LTRIM(RTRIM(w.[Problem])), N'')))))
WHERE w.[ServiceNameSnapshot] IS NULL;
""");
migrationBuilder.CreateIndex(
name: "IX_workOrders_ServiceId",
table: "workOrders",
column: "ServiceId");
migrationBuilder.AddForeignKey(
name: "FK_workOrders_Services_ServiceId",
table: "workOrders",
column: "ServiceId",
principalTable: "Services",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
///
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_workOrders_Services_ServiceId",
table: "workOrders");
migrationBuilder.DropIndex(
name: "IX_workOrders_ServiceId",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ServiceId",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ServiceNameSnapshot",
table: "workOrders");
}
}
}