Align GUID Representation For UUID_TO_BIN and EF Core
Posted by: Daniel King
Date: September 30, 2021 01:46PM

I am trying to understand and fix some odd behavior when using EF Core to retrieve items inserted via direct SQL.

## Version Info

- MySQL version: `8.0.25`
- .NET runtime version: `Core 3.1`
- EF Core version: `5.0.5`
- MySQL.EntityFrameworkCore version: `5.0.5`

## Description

When a UUID is added to the database via direct SQL, it does not match the format of an EF-inserted UUID such that it cannot be retrieved via EF, even when the string representation of the UUID is matches exactly. For example, if we add insert an item directly with the ID `UUD_TO_BIN('831136f0-aee0-47bc-b6ba-be04dc858990')` and then try to find the entity with ID `831136f0-aee0-47bc-b6ba-be04dc858990` EF will return NULL.

Note, this is without configuring EF in any particular manner--the default behavior is misaligned.

Furthermore, the `GuidFormat` connection string option is not supported to allow configuring this. As such there does not appear to be an easy way to fix this misalignment in representation. How can this be addressed or should we file a bug report?

## Example Code

```csharp
var id = Guid.NewGuid();
var idString = id.ToString();
dbContext.Database.ExecuteSqlInterpolated(@$"
INSERT INTO Entities (Id)
VALUES (UUID_TO_BIN({idString}))
");
dbContext.SaveChanges();

dbContext.Entities.Find(id).Should().NotBeNull(); // fails because UUID_TO_BIN represents `id` differently than EF
```

Options: ReplyQuote


Subject
Written By
Posted
Align GUID Representation For UUID_TO_BIN and EF Core
September 30, 2021 01:46PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.