Commit cdc292a
committed
Skip ALTER COLUMN for computed columns when only CLR type changes
The migration model differ produced an AlterColumnOperation when the CLR
type of a property mapped to a computed column changed (e.g. int → long
for a column with .HasComputedColumnSql("DATALENGTH(...)")). The
generated ALTER TABLE ... ALTER COLUMN then failed at runtime with
"Cannot alter column ... because it is 'COMPUTED'".
A computed column's store type and collation are derived from the
expression; they aren't user-configurable. CLR-type-only changes are
metadata on the EF side and require no database-side change. SQL Server
specifically rejects ALTER COLUMN on computed columns altogether.
The fix lives in SqlServerMigrationsSqlGenerator (provider-specific):
when both source and target are computed, set alterStatementNeeded to
false. This suppresses the ALTER COLUMN emission while letting other
separately-emitted facets (notably the comment block at line 488 via
sp_addextendedproperty) still apply. The drop+add path for expression
changes earlier in the method is unchanged.
MigrationsModelDiffer remains provider-independent: it still produces
AlterColumnOperation as before; other providers (MySQL with MODIFY
COLUMN, PostgreSQL, etc.) decide for themselves how to handle it.
Tests:
- Unit: AlterColumnOperation_computed_column_with_only_clr_type_change_is_noop
asserts the generator produces empty SQL for the bug case.
- Unit: AlterColumnOperation_computed_column_with_changed_expression_drops_and_adds
guards the existing drop+add path.
- Functional: Alter_computed_column_clr_type_only_change_is_noop runs the
scenario end-to-end against real SQL Server in the CI matrix; verified
locally against Azure SQL Edge on Apple Silicon.
Fixes #334251 parent a267c3e commit cdc292a
3 files changed
Lines changed: 110 additions & 0 deletions
File tree
- src/EFCore.SqlServer/Migrations
- test/EFCore.SqlServer.FunctionalTests/Migrations
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
357 | 357 | | |
358 | 358 | | |
359 | 359 | | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
360 | 372 | | |
361 | 373 | | |
362 | 374 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
833 | 833 | | |
834 | 834 | | |
835 | 835 | | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
836 | 867 | | |
837 | 868 | | |
838 | 869 | | |
| |||
Lines changed: 67 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
329 | 396 | | |
330 | 397 | | |
331 | 398 | | |
| |||
0 commit comments