Skip to content

SQL Server UTF8 collations #25798

Closed

Description

@egbertn

This does not behave as documented and expected.

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.

Activity

  1. roji commented on Aug 28, 2021

    @roji
    Member

    Can you post a runnable code sample which shows the corruption occurring?

  2. egbertn commented on Aug 29, 2021

    @egbertn
    Author

    Can you post a runnable code sample which shows the corruption occurring?

    Sure,
    there you go. Thanks for your commitment.

    https://github.com/egbertn/EFCore-5-UTF-8-support

  3. self-assigned this
    on Aug 29, 2021
  4. ajcvickers commented on Aug 31, 2021

    @ajcvickers
    Contributor

    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.

    static class Program
    {
    	static void Main()
    	{
    		using (var connection = new SqlConnection(@"Server=localhost;Database=master;Trusted_Connection=True"))
    		{
    			connection.Open();
    
    			using (var command = connection.CreateCommand())
    			{
    				command.CommandText = @"DROP DATABASE [UTF8];";
    				command.ExecuteNonQuery();
    				
    				command.CommandText = @"CREATE DATABASE [UTF8];";
    				command.ExecuteNonQuery();
    			}
    			
    			connection.Close();
    		}
    
    		using (var connection = new SqlConnection(@"Server=localhost;Database=UTF8;Trusted_Connection=True"))
    		{
    			connection.Open();
    
    			using (var command = 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);";
    				
    				var parameter = 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 (var reader = command.ExecuteReader())
    				{
    					reader.Read();
    					var value = reader.GetFieldValue<string>(reader.GetOrdinal("Param"));
    
    					if (value != "Как дела?")
    					{
    						Console.WriteLine($"Wrote {parameter.Value}, read {value}.");
    					}
    				}
    			}
    			
    			connection.Close();
    		}
    	}
    }
  5. ajcvickers commented on Aug 31, 2021

    @ajcvickers
    Contributor

    Notes from triage:

    • 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.
  6. added this to the Backlog milestone on Aug 31, 2021
  7. egbertn commented on Sep 1, 2021

    @egbertn
    Author

    Thanks @ajcvickers

    Notes from triage:

    * 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'как лела?'

  8. roji commented on Sep 1, 2021

    @roji
    Member

    @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.

  9. 22 remaining items

  10. roji commented on Mar 8, 2022

    @roji
    Member

    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...

  11. roji commented on Mar 11, 2022

    @roji
    Member

    Design decisions:

  12. added a commit that references this issue on Mar 13, 2022
    fe312b1
  13. added a commit that references this issue on Mar 17, 2022
    587de30
  14. modified the milestones: 7.0.0, on Mar 31, 2022
  15. changed the title [-]Using SQL Server UTF8 collations[/-] [+]SQL Server UTF8 collations[/+] on Apr 18, 2022
  16. modified the milestones: , 7.0.0 on Nov 5, 2022
  17. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions