Repository navigation
Description
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
SqlServerDifftests: a default-value change, a default-SQL change and a comment change on an indexed column scaffold no index operations.- The cases SQL Server: ALTER COLUMN on a column under a complex index fails at apply (Msg 5074) #84 and SQL Server: a renamed complex index stays in the way of ALTER COLUMN, and the migration fails at apply (Msg 5074) #141 cover still rebuild: a type change,
NULL→NOT NULL, a length change, and a computed-column change. - A
SqlServerIntegrationTestscase that applies the default change with the index in place (it runs in CI only).
Activity
Merge pull request #192 from CaffeinatedCoder/fix/186-sqlserver-alter…
CaffeinatedCoder commentedon Oct 23, 2026
Fixed on release/5.x by #192, shipping in 5.6.0. Closed by hand:
Closesacts only on merges intomain.The SQL Server differ now rebuilds a complex index around an
AlterColumnOperationonly when EF Core's generator renders it asALTER COLUMN(or drops and re-adds a computed column); a default or comment change alone keeps the index.docs: resolve the CLAUDE.md merge conflict, and repair the line it ca…