using Microsoft.EntityFrameworkCore.Migrations; using System; #nullable disable namespace gehGassi.Persistence.Migrations { /// public partial class WalkConfirmationComplaintAdded2 : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AlterColumn( name: "ComplainReason", table: "Walks", type: "nvarchar(500)", maxLength: 500, nullable: true, oldClrType: typeof(string), oldType: "nvarchar(max)", oldNullable: true); var viewName = "tv_WalksWithNames"; var sql = @"CREATE FUNCTION tv_WalksWithNames() RETURNS TABLE AS RETURN SELECT W.*, CONCAT(DW.FirstName, ' ' , DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto, CONCAT(DO.FirstName, ' ' , DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto FROM Walks W LEFT JOIN AppUsers DW ON W.DogWalkerId = DW.Id LEFT JOIN AppUsers DO ON W.DogOwnerId = DO.Id"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO"); migrationBuilder.Sql($"{sql} {Environment.NewLine} GO"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.AlterColumn( name: "ComplainReason", table: "Walks", type: "nvarchar(max)", nullable: true, oldClrType: typeof(string), oldType: "nvarchar(500)", oldMaxLength: 500, oldNullable: true); var viewName = "tv_WalksWithNames"; var sql = @"CREATE FUNCTION tv_WalksWithNames() RETURNS TABLE AS RETURN SELECT W.*, CONCAT(DW.FirstName, ' ' , DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto, CONCAT(DO.FirstName, ' ' , DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto FROM Walks W LEFT JOIN AppUsers DW ON W.DogWalkerId = DW.Id LEFT JOIN AppUsers DO ON W.DogOwnerId = DO.Id"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO"); migrationBuilder.Sql($"{sql} {Environment.NewLine} GO"); } } }