Skip to content

SQL Server: a default value or comment change re-creates every complex index on the column #186

Closed

Description

@CaffeinatedCoder

Found by a sweep of release/5.x @ d28c938 for base operations the differs do not account for (the shape of #182); reproduced in a scratch MigrationHarness.SqlServerDiff test. Not applied to a live server; the SQL below is what EF Core's generator renders with the target model.

Problem

On SQL Server, a change to a column's default value, default SQL or comment drops and re-creates every complex index that reads the column. The database would not have needed it: none of these changes issues an ALTER COLUMN.

The SQL Server differ treats every AlterColumnOperation on a column an index reads as in the way (#84, #141):

// SqlServerComplexIndexMigrationsModelDiffer.cs:66
protected override bool RequiresIndexRecreation(MigrationOperation operation, IReadOnlySet<string> columns)
    => operation is AlterColumnOperation alter && columns.Contains(alter.Name);

EF Core emits an AlterColumnOperation for any column facet that differs. Its SQL Server generator issues ALTER TABLE … ALTER COLUMN only when alterStatementNeeded: type, nullability, collation, length, precision, unicode, fixed length, row version, or a non-identity annotation (SqlServerMigrationsSqlGenerator.cs L346–358 at v10.0.0). A default is rendered as DROP CONSTRAINT plus ADD DEFAULT, and a comment as sp_addextendedproperty. EF Core rebuilds its own indexes only when the column is narrowed (type, collation, or NOT NULL).

Model: bookings with Code (HasMaxLength(20)) and HasComplexCompositeIndex(x => new { x.Code, x.Contact.Email }, indexName: "ix_bookings_code_email"). Add HasDefaultValue("none") to Code:

DROP INDEX [ix_bookings_code_email] ON [bookings];
DECLARE @var nvarchar(max);
SELECT @var = QUOTENAME([d].[name]) FROM [sys].[default_constraints] [d] … WHERE … [c].[name] = N'Code');
IF @var IS NOT NULL EXEC(N'ALTER TABLE [bookings] DROP CONSTRAINT ' + @var + ';');
ALTER TABLE [bookings] ADD DEFAULT N'none' FOR [Code];
CREATE INDEX [ix_bookings_code_email] ON [bookings] ([Code], [Contact_Email]);

HasComment("the booking code") instead gives the same DROP INDEX/CREATE INDEX around a single sp_addextendedproperty. The index is rebuilt offline on every edition but Enterprise, so a one-line metadata change locks the table for as long as the build takes.

Change

Make the SQL Server RequiresIndexRecreation ask what EF Core's generator asks: recreate only when the operation changes something that issues ALTER COLUMN. That means ColumnType (or the CLR type and facets that resolve it), IsNullable, Collation, MaxLength, Precision, Scale, IsUnicode, IsFixedLength, IsRowVersion, ComputedColumnSql/IsStored (dropped and re-added), or a non-identity annotation. Default value, default SQL and comment changes alone do not.

This changes what an existing model scaffolds, so it ships in a minor with a ### Changed answers row and a docs/upgrading.md entry.

Tests

Activity

  1. added this to the 5.6.0 milestone on Oct 23, 2026
  2. added
    bug
    severity/medium
    package/sqlserver
    area/indexes
    area/migrations
    on Oct 23, 2026
  3. added a commit that references this issue on Oct 23, 2026
  4. CaffeinatedCoder commented on Oct 23, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed on release/5.x by #192, shipping in 5.6.0. Closed by hand: Closes acts only on merges into main.

    The SQL Server differ now rebuilds a complex index around an AlterColumnOperation only when EF Core's generator renders it as ALTER COLUMN (or drops and re-adds a computed column); a default or comment change alone keeps the index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/indexesarea/migrationsbugpackage/sqlserverseverity/medium

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions