oracle / dotnet-db-samples

.NET code samples for Oracle database developers #OracleDotNet
http://otn.oracle.com/dotnet
MIT License
413 stars 191 forks source link

Index rebuild during EF Core migration #346

Closed mihaimyh closed 9 months ago

mihaimyh commented 9 months ago

I've recently stumbled on an issue where I had to change a table column type and that resulted in a new Oracle EF Core migration. I've applied the migration but it turned out that also a index rebuild had to be performed, which was not included in the migration. Is there a way to programmatically achieve index rebuilds with EF Core?

alexkeh commented 9 months ago

EF Core itself only supports creating or dropping indexes during migration. Index rebuild is not currently supported. There is an

EF Core rebuild foreign keys feature request that exists.

As a workaround, you can run a custom SQL script by modifying the UP method in the migration class before applying the migration to the database. Here is a reference link:

Custom Migrations Operations

public partial class init : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Schools",
            columns: table => new
            {
                Id = table.Column<int>(type: "NUMBER(10)", nullable: false)
                    .Annotation("Oracle:Identity", "START WITH 1 INCREMENT BY 1"),
                Name = table.Column<string>(type: "NVARCHAR2(2000)", nullable: false),
                Founded = table.Column<DateTime>(type: "TIMESTAMP(7)", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Schools", x => x.Id);
            });
        migrationBuilder.CreateIndex(
            name: "IX_Schools_Founded",
            table: "Schools",
            column: "Founded");
        migrationBuilder.Sql(@" ALTER INDEX ""IX_Schools_Founded"" REBUILD ");
    }
}

Closing the issue for now. I can reopen once EF Core supports the feature, allowing the Oracle provider the opportunity to support it.