You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migrations containing strings with many line-breaks will truncate strings silently #27206
This was originally filed in #24112 and got fixed in #24944.
We first found out about this issue when inserting a large legal text into our database, which contains a lot of line breaks. EF Core generates a large script which uses CONCAT() with up to 254 arguments, before starting a new CONCAT(). (\r and \n get converted to nchar(10) and nchar(13) and will be concatenated with the rest of the text)
As in #24112 described, there is an issue with CONCAT if you don't CAST the first parameter as nvarchar(max) the sql server will assume the 4000 byte limit.
This was fixed with #24944 by adding a CAST to the first parameter of CONCAT. However, subsequent CONCATs will not use a CAST. This will lead to silently truncated strings in the database.
I already had a look at the code and decided to fix it and contribute to this project.
I've faced the same issue while importing html templates via seed, it was truncating the template after a lot of investigation came to know its because of the CAST(0 AS bit), NULL, NULL, CONCAT(CAST(N'<!DOCTYPE html PUBLIC..........
It was working ok in .Net Core 3.1 and 5.0 now we upgraded to .Net 6 and started facing this issue.
PS C:\local\code\repros\StringTruncationSampleEF6-main\StringTruncationSampleEF6-main\WebApplication1> dotnet ef migrations script
Build started...
Build succeeded.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 6.0.1 initialized 'DataContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.1' with options: None
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULLBEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
GO
BEGIN TRANSACTION;
GO
CREATE TABLE [EmailTemplate] (
[Id] intNOT NULL IDENTITY,
[TemplateName] nvarchar(100) NOT NULL,
[TemplateData] nvarchar(max) NOT NULL,
CONSTRAINT [PK_EmailTemplate] PRIMARY KEY ([Id])
);
GO
IF EXISTS (SELECT*FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'TemplateData', N'TemplateName') AND [object_id] = OBJECT_ID(N'[EmailTemplate]'))
SET IDENTITY_INSERT [EmailTemplate] ON;
INSERT INTO [EmailTemplate] ([Id], [TemplateData], [TemplateName])
VALUES (1, CONCAT(CAST(N'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'AS nvarchar(max)), nchar(10), N'<html xmlns="http://www.w3.org/1999/xhtml" style="font-family: ''Helvetica Neue'', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">', nchar(10), N'<head>', nchar(10), N' <meta name="viewport" content="width=device-width">', nchar(10), N' <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">', nchar(10), N' <title>Student Learning Hub</title>', nchar(10), N' <style>', nchar(
10), N' @media only screen and (max-width: 620px) {', nchar(10), N' table[class=body] h1 {', nchar(10), N' font-size: 28px !important;', nchar(10), N' margin-bottom: 10px !important;', nchar(10), N' }', nchar(10), N
' table[class=body] p,', nchar(10), N' table[class=body] ul,', nchar(10), N' table[class=body] ol,', nchar(10), N' table[class=body] td,', nchar(10), N' table[class=body] span,', nchar(10), N' table[class=body] a {', nchar(10), N' font-size: 16px !important;', nchar(10), N' }', nchar(10), N' table[class=body] .wrapper,', nchar(10), N' table[class=body] .article {', nchar(10), N' padding: 10px !important;', nchar(10), N' }', nchar(10), N' table[class=body] .content {', nchar(10), N' padding: 0 !important;', nchar(10), N' }', nchar(10), N' table[class=body] .container {', nchar(10), N' padding: 0 !important;', nchar(10), N' width: 100% !important;', nchar(10), N' }', nchar(10), N' table[class=body] .main {', nchar(10), N' border-left-width: 0 !important;', nchar(10), N' border-radius: 0 !important;', nchar(10), N' border-right-width: 0 !important;', nchar(10), N' }', nchar(10), N' table[class=body] .btn table {', nchar(10), N' width: 100% !important;'
, nchar(10), N' }', nchar(10), N' table[class=body] .btn a {', nchar(10), N' width: 100% !important;', nchar(10), N' }', nchar(10), N' table[class=body] .img-responsive {', nchar(10), N' height: auto !important;', nchar(10), N' max-width: 100% !important;', nchar(10), N' width: auto !important;', nchar(10), N' }', nchar(10), N' }', nchar(10), N' @media all {', nchar(10), N' .ExternalClass {', nchar(10), N' width: 100%;', nchar(10), N' }', nchar(10), N' .ExternalClass,', nchar(10), N' .ExternalClass p,', nchar(10), N' .ExternalClass span,', nchar(10), N' .ExternalClass font,', nchar(10), N' .ExternalClass td,', nchar(10), N' .ExternalClass div {', nchar(10), N' line-height: 100%;', nchar(10), N' }', nchar(10), N' .apple-link a {', nchar(10), N' color: inherit !important;', nchar(10), N' font-family: inherit !important;', nchar(10), N' font-size: inherit !important;', nchar(10), N' font-weight: inherit !important;', nchar(10), N' line-height: inherit !important;', nchar(10), N' text-decoration: none !important;', nchar(10), N' }', nchar(10), N' #MessageViewBody a {', nchar(10), N' color: inherit;', nchar(10), N' text-decoration: none;', nchar(10), N' font-size: inherit;', nchar(10), N' font-family: inherit;', nchar(10), N' font-weight: inherit;', nchar(10), N' line-height: inherit;', nchar(10), N' }', nchar(10), N' .btn-primary table td:hover {', nchar(10), N' background-color: #34495e !important;', nchar(10), N' }', nchar(10), N
' .btn-primary a:hover {', nchar(10), N' background-color: #34495e !important;', nchar(10), N' border-color: #34495e !important;', nchar(10), N' }', nchar(10), N' }', nchar(10), N' </style>', nchar
(10), N'</head>', nchar(10), N'<body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">', nchar(10), N' <table border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background-color: #f6f6f6;">', nchar(10), N' <tr>', nchar(10), N' <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;"> </td>',
nchar(10), N' <td class="container" style="font-family: sans-serif; font-size: 14px; vertical-align: top; display: block; Margin: 0 auto; max-width: 580px; padding: 10px; width: 580px;">', nchar(10), N' <div class="content" style="box-sizing: border-box; display: block; Margin: 0 auto; max-width: 580px; padding: 10px;">', nchar(10), N' <span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">This is preheader text. Some clients will show this text as a preview.</span>', nchar(10), N' <table class="main" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; background: #ffffff; border-radius: 3px;">', nchar(10), N' <tr style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; font-weight:500;">', nchar(10), N' <td class="alert alert-warning" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; color:#00468C; font-weight:600; text-align: center; border-radius: 3px 3px 0 0; background-color:#fff ; margin: 0; padding: 20px;" align="center" bgcolor="#FF9F00" valign="top">', nchar(10), N' Email Confirmation', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' <tr>', nchar(10), N' <td class="wrapper" style="font-family: sans-serif; font-size: 14px; vertical-align: top; box-sizing: border-box; padding: 20px;">', nchar(10), N' <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">', nchar(10), N' <tr>', nchar(10), N' <td class="alert alert-warning">', nchar(10), N' <hr noshade size="2" style="background-color: #00468C" />', nchar(10), N' <br />', nchar(10), N' <p class="content-block" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">', nchar(10), N' Please confirm your email address by clicking the link below.', nchar(10), N' </p>', nchar(10), N' <table border="0" cellpadding="0" cellspacing="0" class="btn btn-primary" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%; box-sizing: border-box;">', nchar(10), N' <tbody>', nchar(10), N' <tr style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">', nchar(10), N' <td class="content-block" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">', nchar(10), N' We may need to send you critical information about our service and it is important that we have an accurate email address.', nchar(10), N' </td>',
nchar(10), N' </tr>', nchar(10), N' <tr style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">', nchar(10), N' <td class="content-block" itemprop="handler" itemscope=itemscope itemtype="http://schema.org/HttpActionHandler" style="font-family: ''HelveticaNeue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">', nchar(10), N' <a href="[[Link]]" class="btn-primary" itemprop="url" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; color: #FFF; text-decoration: none; line-height: 2em; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 5px; text-transform: capitalize; background-color: #348eda; margin: 0; border-color: #348eda; border-style: solid; border-width: 10px 20px;">Confirm email address</a>', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' <tr style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">', nchar(10)
, N' <td class="content-block" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">'
, nchar(10), N' Or you can call admin office and share this OTP for confirmation.', nchar(10), N' <p class="content-block" style="color: dodgerblue; font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">', nchar(10), N' <b>[[OTP]]</b>', nchar(10), N' </p>', CONCAT(nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' </tbody>', nchar(10), N' </table>', nchar(10), N' <br />', nchar(10), N' </td>', nchar(10
), N' </tr>', nchar(10), N' <tr style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">', nchar(10), N' <td class="content-block" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">', nchar(10), N' Email our support team at <a href="mailto:[[SupportEmail]]">[[SupportEmail]]</a>, or call us on <a href="tel:[[SupportPhone]]">[[SupportPhone]]</a> for any queries.', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' <tr style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">', n
char(10), N' <td class="content-block" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">', nchar(10), N' <b>Best Wishes</b> <br />', nchar(10), N' <b>Testing System </b>', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' </table>', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' <tr>', nchar(10), N' <td class="aligncenter content-block" style="font-family: ''Helvetica Neue'',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 12px;', nchar(10), N' vertical-align: top; color: #fff; text-align: left; margin: 0; padding: 20px; border-bottom: solid 4px #0b0b0b; background:#00468C" bgcolo="#00468C" align="center" valign="top">', nchar(10), N' The Test System is your one-stop-shop for all undergraduate learning supports and resources. Visit us early and often to build the skills, strategies, and behaviours that are essential to become a confident and independent learner.<br /><br />', nchar(10), N' c [[Year]] Test System. All rights reserved.', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' </table>', nchar(10), N' <div class="footer" style="clear: both; Margin-top: 10px; text-align: center; width: 100%;">', nchar(10), N' <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;">', nchar(10), N' <tr>', nchar(10), N' <td class="content-block" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; font-size: 12px; color: #999999; text-align: center;">', nchar(10), N' <span class="apple-link" style="color: #999999; font-size: 12px; text-align: center;">The Testing System</span>', nchar(10), N' </td>', nchar(10), N' </tr>', nchar(10), N' </table>', nchar(10), N' </div>', nchar(10), N' </div>', nchar(10), N' </td>', nchar(10), N' <td style="font-family: sans-serif; font-size: 14px; vertical-align: top;"> </td>', nchar(10), N' </tr>', nchar(10), N' </table>', nchar(10), N'</body>', nchar(10), N'</html>')), N'Email Confirmation');
IF EXISTS (SELECT*FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'TemplateData', N'TemplateName') AND [object_id] = OBJECT_ID(N'[EmailTemplate]'))
SET IDENTITY_INSERT [EmailTemplate] OFF;
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20220218195810_One', N'6.0.1');
GO
COMMIT;
GO
Not for triage: confirmed that only the first CONCAT has a CAST. This means that only around 85 lines of text can be included (since CR/LF are expanded) until the issue reocurs.
Activity
dnxit commentedon Feb 9, 2022
I've faced the same issue while importing html templates via seed, it was truncating the template after a lot of investigation came to know its because of the CAST(0 AS bit), NULL, NULL, CONCAT(CAST(N'<!DOCTYPE html PUBLIC..........
It was working ok in .Net Core 3.1 and 5.0 now we upgraded to .Net 6 and started facing this issue.
AndriySvyryd commentedon Feb 15, 2022
@dnxit Can you share a small repro project so we can make sure that you are hitting the same issue?
dnxit commentedon Feb 17, 2022
@AndriySvyryd yeah sure, I've created a sample. just set the connection string and run the sample, by the way I've added generated query from SQL profiler as well.
https://github.com/dnxit/StringTruncationSampleEF6
ajcvickers commentedon Feb 18, 2022
SQL script:
ajcvickers commentedon Feb 21, 2022
Simplified repro:
Not for triage: confirmed that only the first CONCAT has a CAST. This means that only around 85 lines of text can be included (since CR/LF are expanded) until the issue reocurs.
C#
Add more cast nodes in multi-line breaks during migrations
6 remaining items
ajcvickers commentedon Aug 2, 2022
/cc @smitpatel
Add more cast nodes in multi-line breaks during migrations
Add more cast nodes in multi-line breaks during migrations (#28576)
CONCATusage. #27271 as a duplicate of this issue