Skip to content

Emit N prefix for xml string literals in SQL Server#38460

Closed
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-xml-constants-n-prefix
Closed

Emit N prefix for xml string literals in SQL Server#38460
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-xml-constants-n-prefix

Conversation

Copilot AI commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

String constants mapped to the SQL Server xml store type were generated without the N prefix, so they were treated as non-Unicode varchar. The database collation's codepage was then applied, silently corrupting characters outside it (e.g. emoji in XML attribute values).

-- before
SELECT COALESCE([m].[SomeXml], '<x Attr="😭" />') FROM [MyEntities] AS [m]
-- after
SELECT COALESCE([m].[SomeXml], N'<x Attr="😭" />') FROM [MyEntities] AS [m]

Changes

  • SqlServerStringTypeMapping: extend the _isUtf16 detection to also recognize the xml store type. The xml mapping is created with unicode: true but a store type of "xml", which the existing StartsWith("n") guard excluded.
  • Kept the StartsWith("n") guard (rather than reverting to plain parameters.Unicode) to preserve its original intent: a property mapped as Unicode but with an explicit non-Unicode store type (e.g. varchar) must not get an N prefix. xml is genuinely a Unicode store type, so it is added alongside n*.
_isUtf16 = parameters.Unicode
    && (parameters.StoreType.StartsWith("n", StringComparison.OrdinalIgnoreCase)
        || parameters.StoreType.StartsWith("xml", StringComparison.OrdinalIgnoreCase));

This also propagates to the related literal pieces (nchar(10/13) and CAST(... AS nvarchar(max)) inside CONCAT).

Tests

  • Added GenerateProviderValueSqlLiteral_works_xml covering plain, escaped-apostrophe, emoji, and line-feed/CONCAT cases to confirm xml literals now emit N'...', nchar, and nvarchar(max).

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix missing N prefix for xml constants in queries Emit N prefix for xml string literals in SQL Server Jun 18, 2026
Copilot AI requested a review from AndriySvyryd June 18, 2026 17:41
_isUtf16 = parameters.Unicode && parameters.StoreType.StartsWith("n", StringComparison.OrdinalIgnoreCase);
_isUtf16 = parameters.Unicode
&& (parameters.StoreType.StartsWith("n", StringComparison.OrdinalIgnoreCase)
|| parameters.StoreType.StartsWith("xml", StringComparison.OrdinalIgnoreCase));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Equals("xml", StringComparison.OrdinalIgnoreCase)) ?

@AndriySvyryd

Copy link
Copy Markdown
Member

Superseded by #38462

@AndriySvyryd AndriySvyryd deleted the copilot/fix-xml-constants-n-prefix branch June 19, 2026 01:54
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.

3 participants