Altering column with index on it in Entity Framework Core

entity-framework entity-framework-core

Question

Version

SQL enginie: MS SQL.

EF Core packages versions:

"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"

Problem

I've changed the existing column, which had index on it created few migrations earlier:

// ... other changes
migrationBuilder.CreateIndex(
    name: "IX_Interactions_OrganisationToId",
    table: "Interactions",
    column: "OrganisationToId");

I've changed it from optional to required (not-nullable). Entity Framework Core generated such a migration:

// ... other changes
migrationBuilder.AlterColumn<Guid>(
    name: "OrganisationToId",
    table: "Interactions",
    nullable: false);

But you cannot alter the column which has index on it. Unfortunately EF Core is not supporting this in case of changing field to non-nullable.

When renaming field - works like a charm and before rename simply drops index and restores it after alter.


Question

Is there any way to configure EF Core that he'll cover this case and generate correct migration?

1
1
11/18/2016 1:14:37 AM

Accepted Answer

At the moment I've modified the migration and dropped down the index before altering the column, and manually restore it after the operation.

It did the job.

// drop index before altering column
migrationBuilder.DropIndex(name: "IX_Interactions_OrganisationToId", table: "Interactions");

// actually altering column
migrationBuilder.AlterColumn<Guid>(
   name: "OrganisationToId",
   table: "Interactions",
   nullable: false);

// restore index
migrationBuilder.CreateIndex(
   name: "IX_Interactions_OrganisationToId",
   table: "Interactions",
   column: "OrganisationToId");
2
11/18/2016 12:45:37 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow