I am using visual studio, EF Core
I want to take my project and publish it to production I understand that Migrations Is not a good way to work with on a production I think the best way to update my database it is to make a Migration on the developing version and create a SQL out of it and execute the SQL on the production
I have open this question : https://stackoverflow.com/questions/66966860/get-sql-file-for-specific-migration-in-entity-framework-core-c-sharp
But some of the scripts return empty I have no idea why...
for example :
I have the next file : 20210314131832_RemoveCitizenShip2.cs
with the next code :
using Microsoft.EntityFrameworkCore.Migrations;
namespace CertificateSystem.DB.DB.Migrations
{
public partial class RemoveCitizenShip2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CitizenShip",
schema: "CertificateSystem",
table: "CommercialEntity");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "CitizenShip",
schema: "CertificateSystem",
table: "CommercialEntity",
nullable: true);
}
}
}
execute : Script-Migration 20210314131832_RemoveCitizenShip2 just work fine
and return the next SQL Script :
ALTER TABLE [CertificateSystem].[CommercialEntity] ADD [CitizenShip] int NULL;
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20210314132027_AddBackCitizenShip2', N'2.2.4-servicing-10062');
GO
DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[CertificateSystem].[CommercialEntity]') AND [c].[name] = N'CitizenShip');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [CertificateSystem].[CommercialEntity] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [CertificateSystem].[CommercialEntity] DROP COLUMN [CitizenShip];
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20210314142045_RemoveCitizenShip3', N'2.2.4-servicing-10062');
GO
I also have the next file : 20210406095255_ChangeUidToInt.cs
with the code:
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace OnlineShvaConnection.DB.Db.Migrations
{
public partial class ChangeUidToInt : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<long>(
name: "Id",
schema: "OnlineShvaConnection",
table: "LogMessages",
nullable: false,
oldClrType: typeof(Guid))
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<Guid>(
name: "Id",
schema: "OnlineShvaConnection",
table: "LogMessages",
nullable: false,
oldClrType: typeof(long))
.OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
}
}
}
and it return a empty SQL file on the next command line :
Script-Migration 20210406095255_ChangeUidToInt
I have no idea why it can happen...
Script-Migration -Idempotent, it applies the missing migrations. This is useful if you don't exactly know what the last migration applied to the database was. you can also try with options-fromand-to