Bug #105142 | UUID Binary Representation For UUID_TO_BIN and EF Core Is Incompatible | ||
---|---|---|---|
Submitted: | 5 Oct 2021 19:31 | Modified: | 24 Nov 2022 1:52 |
Reporter: | Daniel King | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | Connector / NET | Severity: | S2 (Serious) |
Version: | 8.0.25, 8.0.26 | OS: | Any |
Assigned to: | CPU Architecture: | x86 |
[5 Oct 2021 19:31]
Daniel King
[6 Oct 2021 6:43]
MySQL Verification Team
Hello Daniel, Thank you for the report and test case. regards, Umesh
[24 Nov 2022 1:52]
Daniel Valdez
Posted by developer: Hi Daniel, When GUID is converted to a byte array so it can be written in the stream and then stored in the database, the order is swapped as you noticed but this is as per MS specification (https://learn.microsoft.com/en-us/dotnet/api/system.guid.tobytearray?view=net-7.0#remarks), so there's no error in that regards. Respecting the query you're testing to retrieve the values from the table, you're missing something and hence the "bug"; it is the HEX() function (https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_hex). So in the end your code will look like this: ```csharp var id = Guid.NewGuid(); dbContext.Entities.Add(new Entity { Id = id }); dbContext.SaveChanges(); var selected = dbContext.Entities.FromSqlInterpolated(@$" SELECT * FROM Entities WHERE Id = UUID_TO_BIN(HEX({id})); ").AsEnumerable().FirstOrDefault(); selected.Should().NotBeNull(); ``` Verified using the latest version of MySQL Connector/NET, v8.0.31. Thanks, Daniel V
[25 Apr 2024 21:47]
Rafael Santos
That function may work to reorder the bytes : " public static byte[] ToMySqlOrderedBytes(byte[] binUuid) { byte[] originalBytes = binUuid; byte[] orderedBytes = new byte[16]; // Reorder bytes according to the specified sequence // Reverse the first 4 bytes orderedBytes[0] = originalBytes[3]; orderedBytes[1] = originalBytes[2]; orderedBytes[2] = originalBytes[1]; orderedBytes[3] = originalBytes[0]; // Reverse the next 2 bytes orderedBytes[4] = originalBytes[5]; orderedBytes[5] = originalBytes[4]; // Reverse the next 2 bytes orderedBytes[6] = originalBytes[7]; orderedBytes[7] = originalBytes[6]; // Copy the remaining 8 bytes as they are (positions 8-15) Array.Copy(originalBytes, 8, orderedBytes, 8, 8); return orderedBytes; } "