using Microsoft.EntityFrameworkCore.Migrations; using System; #nullable disable namespace gehGassi.Persistence.Migrations { public partial class BranchesOrderAdded : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "Order", table: "Branches", type: "int", nullable: false, defaultValue: 0); var viewName = "tv_BranchesWithNames"; var sql = @"CREATE FUNCTION tv_BranchesWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Branches.*, Isnull(JSON_VALUE(dbo.Branches.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Branches.NameLocalized, '$.' + @fallback)) AS Name, Isnull(JSON_VALUE(dbo.Branches.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Branches.DescriptionLocalized, '$.' + @fallback)) AS Description, JSON_VALUE(dbo.Branches.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage FROM dbo.Branches"; 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.DropColumn( name: "Order", table: "Branches"); var viewName = "tv_BranchesWithNames"; var sql = @"CREATE FUNCTION tv_BranchesWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Branches.*, Isnull(JSON_VALUE(dbo.Branches.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Branches.NameLocalized, '$.' + @fallback)) AS Name, Isnull(JSON_VALUE(dbo.Branches.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Branches.DescriptionLocalized, '$.' + @fallback)) AS Description, JSON_VALUE(dbo.Branches.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage FROM dbo.Branches"; 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"); } } }