Fix alter column type JSON to nVarchar#38464
Conversation
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
Pull request overview
Fixes SQL Server migrations for ALTER COLUMN when converting a column from the native json type to nvarchar(...), which SQL Server doesn’t allow via implicit conversion. The SQL generator now emits a rename → add → CONVERT copy → drop flow instead of a direct ALTER COLUMN.
Changes:
- Updated
SqlServerMigrationsSqlGeneratorto special-casejson→non-jsontype changes and generate a safe copy-based migration sequence. - Added SQL generation tests validating the emitted DDL/DML for
json→nvarchar(max)(nullable, non-nullable, idempotent) and thatnvarchar→jsonstill usesALTER COLUMN. - Added a functional test covering runtime execution for
json→nvarchar(max)conversion (nullable case).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs | Implements rename/add/convert-copy/drop workaround for json→non-json ALTER COLUMN on SQL Server. |
| test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs | Adds SQL assertion tests for the new generation path and ensures nvarchar→json remains unchanged. |
| test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs | Adds an end-to-end functional test executing the json→nvarchar(max) workaround. |
|
Hi @AndriySvyryd
Could you please take another look when you get a chance? One note on running the tests locally: Version.Details.props references 11.0.0-preview.6.26313.102 which isn't publicly available yet, so the test runner fails to resolve the runtime. I was running tests against preview.5 locally as a workaround. This should not affect the PR itself but wanted to flag it in case it causes issues in CI. |
…b.com/atiq-bs23/efcore into fix-json-to-nvarchar-column-migration
Fixes #38364
SQL Server's native
jsontype cannot be implicitly converted tonvarcharinALTER COLUMN. When the old column type isjson, the generator now uses rename → add → CONVERT-copy → drop instead.