Skip to content

Use string type mapping when sending null values for JSON column update on SQL Server#38426

Open
Copilot wants to merge 5 commits into
mainfrom
copilot/fix-json-column-update-date-fields
Open

Use string type mapping when sending null values for JSON column update on SQL Server#38426
Copilot wants to merge 5 commits into
mainfrom
copilot/fix-json-column-update-date-fields

Conversation

Copilot AI commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

On SQL Server, setting a nullable date/time property (DateTime?, DateOnly?, etc.) inside a JSON-mapped owned entity to null failed with Argument data type date is invalid for argument 3 of json_modify function (SqlException 8116). EF emitted the JSON_MODIFY parameter using the property's native date/datetime type mapping, which JSON_MODIFY rejects — even when the value is null.

entity.Reference.NullDateTimeProp = null;
await context.SaveChangesAsync(); // threw SqlException 8116

Changes

  • SqlServerModificationCommand.ProcessSinglePropertyJsonUpdate: when the value is null, retain the property's type mapping only for types JSON_MODIFY accepts directly (string, bool, numeric); otherwise fall back to nvarchar(max) (SqlServerStringTypeMapping.UnicodeDefault). The rendered NULL parameter is independent of its declared type, so results are unchanged for the previously-working cases.

    if (value is null)
    {
        parameters = parameters with
        {
            TypeMapping = propertyProviderClrType == typeof(string)
                || propertyProviderClrType == typeof(bool)
                || propertyProviderClrType.IsNumeric()
                    ? mapping
                    : SqlServerStringTypeMapping.UnicodeDefault
        };
        return;
    }
  • Test model: added TestNullableDateTime / TestNullableDateOnly to the shared JSON all-types model, seed data, and AssertAllTypes.

  • Tests: added Edit_single_property_nullable_datetime_set_to_null and Edit_single_property_nullable_dateonly_set_to_null to JsonUpdateTestBase, with provider SQL baselines for Sqlite, SQL Server, and the SQL Server JSON-type fixture. Updated the two full-document-serialization baselines affected by the new model properties.

Notes

  • Sqlite is unaffected — json_set accepts null parameters of any type — so no provider change was needed there; tests confirm correct behavior.
  • SQL Server and JSON-type baselines were derived analytically (no SQL Server instance available in this environment) and warrant CI validation.

Copilot AI and others added 3 commits June 13, 2026 00:44
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix JSON column update failure for date-related fields Fix JSON column update failure for nullable date/time fields set to null on SQL Server Jun 13, 2026
Copilot AI requested a review from AndriySvyryd June 13, 2026 01:12
@AndriySvyryd AndriySvyryd requested a review from Copilot June 13, 2026 01:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a SQL Server-specific failure when updating JSON-mapped owned properties to null for date/time CLR types (e.g., DateTime?, DateOnly?). The failure was caused by sending a NULL parameter with an underlying SQL type (e.g., date) that JSON_MODIFY rejects, even when the value is NULL.

Changes:

  • Adjust SQL Server JSON single-property update parameter type mapping when the value is null, falling back to nvarchar(max) for types not accepted by JSON_MODIFY.
  • Extend the shared JSON “all types” owned model with TestNullableDateTime and TestNullableDateOnly, including seeding and assertions.
  • Add/update provider functional tests and SQL baselines for nulling these JSON properties (SqlServer, SqlServer json-type fixture, Sqlite).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/EFCore.SqlServer/Update/Internal/SqlServerModificationCommand.cs Changes null-parameter type mapping for JSON single-property updates to avoid JSON_MODIFY type rejection.
test/EFCore.Relational.Specification.Tests/Update/JsonUpdateTestBase.cs Adds new base tests for setting nullable date/time JSON properties to null.
test/EFCore.SqlServer.FunctionalTests/Update/JsonUpdateSqlServerTest.cs Adds SQL Server baselines for the new nulling tests and updates affected baselines.
test/EFCore.SqlServer.FunctionalTests/Update/JsonUpdateJsonTypeSqlServerTest.cs Adds SQL Server (json-type fixture) baselines for the new nulling tests and updates affected baselines.
test/EFCore.Sqlite.FunctionalTests/Update/JsonUpdateSqliteTest.cs Adds SQLite baselines for the new nulling tests and updates affected baselines.
test/EFCore.Specification.Tests/TestModels/JsonQuery/JsonOwnedAllTypes.cs Adds nullable DateTime/DateOnly properties to the JSON owned type model.
test/EFCore.Specification.Tests/TestModels/JsonQuery/JsonQueryData.cs Seeds values for the new nullable properties in the shared JSON all-types data.
test/EFCore.Specification.Tests/Query/JsonQueryFixtureBase.cs Extends AssertAllTypes to include the new nullable date/time properties.

Comment thread test/EFCore.Specification.Tests/TestModels/JsonQuery/JsonQueryData.cs Outdated
Comment thread test/EFCore.Specification.Tests/TestModels/JsonQuery/JsonQueryData.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AndriySvyryd AndriySvyryd changed the title Fix JSON column update failure for nullable date/time fields set to null on SQL Server Use string type mapping when sending null values for JSON column update on SQL Server Jun 13, 2026
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 13, 2026 04:17
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 13, 2026 04:18
Copilot AI review requested due to automatic review settings June 13, 2026 04:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@AndriySvyryd AndriySvyryd requested a review from cincuranet June 15, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants