gehgassi_backend/gehGassi.Persistence/Migrations/20221026145724_WalkWithNamesAdded.cs

32 lines
1.4 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
using System;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
public partial class WalkWithNamesAdded : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
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)
{
var viewName = "tv_WalksWithNames";
migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO");
}
}
}