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
If I have a entity for which I use Fluent API to define properties...
The SQL field (in my example) is varchar(255) using collation Latin1_General_100_BIN2_UTF8
in EF defined as p.Property(prop => prop.Param).IsUnicode(false).UseCollation("Latin1_General_100_BIN2_UTF8").HasMaxLength(255);
However, unicode chars get's corrupted anyway on SQL both on Azure as on 2019 express.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: b27be469-3758-941c-0c5d-3a54b1545493
Version Independent ID: d89924aa-668e-229c-8170-5731c3fff4c1
Note for triage: this is because the DbType is set to AnsiString when IsUnicode is false. Below is a repro without EF Core. Changing the DbType to String makes it work.
Not using IsUnicode(false) makes the test pass, but creates an nvarchar(255) column, which apparently isn't desirable for SQL Server's squirrely UTF8 support.
staticclassProgram{staticvoidMain(){using(varconnection=newSqlConnection(@"Server=localhost;Database=master;Trusted_Connection=True")){connection.Open();using(varcommand=connection.CreateCommand()){command.CommandText=@"DROP DATABASE [UTF8];";command.ExecuteNonQuery();command.CommandText=@"CREATE DATABASE [UTF8];";command.ExecuteNonQuery();}connection.Close();}using(varconnection=newSqlConnection(@"Server=localhost;Database=UTF8;Trusted_Connection=True")){connection.Open();using(varcommand=connection.CreateCommand()){command.CommandText=@" CREATE TABLE [MyTable] ( [Id] bigint NOT NULL IDENTITY, [Param] varchar(255) COLLATE Latin1_General_100_BIN2_UTF8 NULL, CONSTRAINT [PK_MyTable] PRIMARY KEY ([Id]) );";command.ExecuteNonQuery();command.CommandText=@"INSERT INTO [MyTable] ([Param]) VALUES (@p0);";varparameter=command.CreateParameter();parameter.DbType=DbType.AnsiString;parameter.Size=255;parameter.ParameterName="p0";parameter.Value="Как дела?";command.Parameters.Add(parameter);command.ExecuteNonQuery();command.CommandText=@"SELECT TOP(1) [m].[Id], [m].[Param] FROM [MyTable] AS [m] ORDER BY [m].[Id] DESC";using(varreader=command.ExecuteReader()){reader.Read();varvalue=reader.GetFieldValue<string>(reader.GetOrdinal("Param"));if(value!="Как дела?"){Console.WriteLine($"Wrote {parameter.Value}, read {value}.");}}}connection.Close();}}}
* It's not clear how to fix this; it will likely require type mappings to understand collations, which is not trivial.
* For now, the workaround is to use `IsUnicode(true)` (which is the default) and to edit the migration to generate a `varchar` column.
Thanks, but isn't it a solution, to see if the collation name contains or ends with _utf8, if so, all string operations must be prepended with N like in N'как лела?'
@egbertn that could be a fix, but it would "require type mappings to understand collations, which is not trivial" as @ajcvickers wrote above. It's unfortunately too late to go into something like this for 6.0.
I hope so - at least the "decomposed version" (i.e. with HasColumnType+IsUnicode+UseCollation), if not the nicer UseUTF8 version... I'll see when I get to implementing it...
Activity
roji commentedon Aug 28, 2021
Can you post a runnable code sample which shows the corruption occurring?
egbertn commentedon Aug 29, 2021
Sure,
there you go. Thanks for your commitment.
https://github.com/egbertn/EFCore-5-UTF-8-support
ajcvickers commentedon Aug 31, 2021
Note for triage: this is because the
DbTypeis set toAnsiStringwhenIsUnicodeis false. Below is a repro without EF Core. Changing theDbTypetoStringmakes it work.Not using
IsUnicode(false)makes the test pass, but creates annvarchar(255)column, which apparently isn't desirable for SQL Server's squirrely UTF8 support.ajcvickers commentedon Aug 31, 2021
Notes from triage:
IsUnicode(true)(which is the default) and to edit the migration to generate avarcharcolumn.egbertn commentedon Sep 1, 2021
Thanks @ajcvickers
Thanks, but isn't it a solution, to see if the collation name contains or ends with _utf8, if so, all string operations must be prepended with N like in N'как лела?'
roji commentedon Sep 1, 2021
@egbertn that could be a fix, but it would "require type mappings to understand collations, which is not trivial" as @ajcvickers wrote above. It's unfortunately too late to go into something like this for 6.0.
22 remaining items
roji commentedon Mar 8, 2022
I hope so - at least the "decomposed version" (i.e. with HasColumnType+IsUnicode+UseCollation), if not the nicer UseUTF8 version... I'll see when I get to implementing it...
roji commentedon Mar 11, 2022
Design decisions:
HasColumnType("varchar(max)").IsUnicode(true))Support SQL Server UTF8 strings
Support SQL Server UTF8 strings (#27634)
[-]Using SQL Server UTF8 collations[/-][+]SQL Server UTF8 collations[/+]